in reply to Perl and Threading

Do you detach or join all the threads?

Do you use threads::shared?

Do you use bare threads, or a wrapper like Thread::Queue? I highly recommend using it.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Perl and Threading
by scorpion7 (Novice) on Nov 02, 2023 at 23:03 UTC
    Using Thread::Queue.
    
    Logic:
    - Collect list of things to process (into a Thread::Queue)
    - Execute threads using Thread::Queue for loop
    - Each thread has what it needs to process a Queue item
    
    Using this style structure:
    @threads=map {
      threads->create(sub {
      (all things required for each thread to process a Thread::Queue item via a while(defined()) { } structure)
      });
      } 1 .. <numthreads>;
    $_->join for @threads
    
    Not using the threads:shared.  (Looked at it a couple times, but not sure that I've encountered a use case for it -or- simply don't have sufficiently complex code to justify?)
    
    Thanks!
    
      Are you using any modules in your script? Note that many modules aren't thread safe or need special handling to stay like that.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        DBI is in use. Each thread has it's own local prepared statements for insert/update for the data its processing. eg: have tried to handle everything as each thread having it's own local information/variables/etc. and not shared across threads, per se.