in reply to Help needed with regard to arrays

Instead of creating a new thread, create 10 threads which take their work from a central queue:

my $threadcount = 10; my $jobs = Thread::Queue->new(@xyz); # I use undef as marker when to stop working $jobs->enqueue(undef) for 1..$threadcount; my @workers = map { threads->create( \&work ) } 1..$threadcount; sub work { while (defined (my $item = $jobs->dequeue)) { print "Processing $item\n"; }; };

But maybe I'm misunderstanding the problem you're trying to solve.

Replies are listed 'Best First'.
Re^2: Help needed with regard to arrays
by theknightsofni (Novice) on Nov 20, 2008 at 19:17 UTC
    Hi Corion, With regards to your reply with the thread code, I tried it out and it appears that the threads are asynchronous.....is there a way to make 10 concurrent threads using Thread::Queue? Thanks

      I don't know what you mean by your usage of "asynchronous" as seemingly contrary to "concurrent". Maybe you can describe what problem you're trying to solve or what problem you encounter. Maybe part of the problem is that the sample code doesn't actually spend much time in the threads and hence each thread finishes too quickly. Maybe you want to add a random sleep to each thread.

        So I plugged in your code to the script that I am writing which ssh's to different systems and runs a tasks. I want it to be able create 10 simultaneous ssh threads to 10 different systems (hence the array with 10 hostnames in the array). With your Thread::Queue code, the ssh is not concurrent (if there is a server that is not available for instance, the ssh command sits there till it times out before moving to the next one). Does this make sense?