in reply to Why is my counter not updating in a threaded application?

You are not threading, you are forking. Your incrementing of the counter only happens in the child.

If you move the counter before the $pm->start and next LOOP, it will increment properly as that runs in the main process:

our $ctr = 0; say "processing duplicates..."; LOOP: foreach my $pid (@duplicated) { unless ($ctr % 1000) {say $ctr;} $ctr++; $pm->start and next LOOP; # do the fork #do stuff $pm->finish; # exit the child process }

Replies are listed 'Best First'.
Re^2: Why is my counter not updating in a threaded application?
by cormanaz (Deacon) on Mar 29, 2024 at 20:47 UTC
    I realized I probably should have done that after posting this. But I thought child processes have access to the global variables. Does this mean anything I push onto our @results; (defined before this code segment) in the #do stuff section won't be retained either?

      I thought child processes have access to the global variables.

      The child process is a copy of the parent process, made at the point of fork(). So any variables (not just globals) will have the same value at the start of the child process as they did in the parent process at that time. But changes made in the child affect only the copy, not the original in the parent.

      The same is true for threads, except that in that case there is a special mechanism called sharing that lets you nominate specific variables to be shared across threads, such that a change made in one thread is visible in the other threads (see threads::shared). This is costly, however - access to such variables must be protected by locking - so should be used sparingly if you hope to retain any speed benefit from making an application multi-threaded.

      There are other more explicit mechanisms for sharing data between threads or processes - think for example of pipes, shared memory, or databases. Collectively such mechanisms are known as "IPC" (inter-process communication) - there are lots of modules in the IPC:: namespace that may give you ideas.