Noob threading question

S

Steve Linberg

OK, OK, I've never really messed with threads and suddenly have a reason
to. I'm trying to grok it all and get a little sample code going that
carries out some basic mechanics, and I'm having one problem that is
probably very simple but I can't figure it out. Any dope-slaps
appreciated, I've FAQ'd and Googled and Camel'ed for a few hours to no
avail so far.

This sample code launches $num_threads threads via async that just count
to random numbers, and get collected by the parent. That part works
fine, the threads spawn, do their thing, and end, but what I can't get
them to do, with or without locking, is increment a lexical (or global)
variable defined above their scope.

I just want the async process to modify something above it. The original
idea was to have the main process wait until all of the threads were
done before collecting them, and in this case therefore, waiting until
$proc_count reached $num_threads, but $proc_count never gets
incremented. Or, rather, it does, but only in the scope of the thread,
and then it "resets". I've also tried dereferencing $proc_count inside
the thread, but the same thing happens.

I must be doing something dumb or making a dumb and false assumption,
but I can't see it. Argh.

Here's the code:
-------------------------------------------------------
#!/usr/bin/perl -w

use strict;
use Thread qw/async yield/;

my @t = ();
my $num_procs = 3;
my $proc_count = 0;

for my $i (1..$num_procs) {
push @t, async {
my $num = int(rand(100000));
my $j = 0;
do {
$j++;
yield unless $j % 1000;
} until ($j > $num);
print qq(Thread $i is done.\n);
{
lock $proc_count;
$proc_count++;
print qq(proc_count inside thread $i: $proc_count\n);
}
qq(Thread $i counted to $num\n);
};
}

for (@t) {
my $ret = $_->join();
print $ret;
}
print qq(proc_count = $proc_count\n);
-------------------------------------------------------

Here's some sample output:

-------------------------------------------------------
Thread 1 is done.
proc_count inside thread 1: 1
Thread 1 counted to 13191
Thread 3 is done.
proc_count inside thread 3: 1
Thread 2 is done.
proc_count inside thread 2: 1
Thread 2 counted to 70326
Thread 3 counted to 35370
proc_count = 0
-------------------------------------------------------

With or without the "lock" of $proc_count, it's always zero at the end.

What am I doing wrong?

TIA for any ideas.
 
S

Steve Linberg

Aukjan van Belkum said:
This is because you are not using shared threads (use threads::shared).

You should read:

perldoc perlthrtut
perldoc threads::shared

Ah! Thanks for the tip.
 
B

Ben Morrow

Quoth Steve Linberg said:
OK, OK, I've never really messed with threads and suddenly have a reason
to. I'm trying to grok it all and get a little sample code going that
carries out some basic mechanics, and I'm having one problem that is
probably very simple but I can't figure it out. Any dope-slaps
appreciated, I've FAQ'd and Googled and Camel'ed

The Camel will be no help here. The threading model described there
(5005threads) is obsolete and no longer part of perl. Read perl's own
docs (perlthrtut, first).
Here's the code:
-------------------------------------------------------
#!/usr/bin/perl -w

use strict;
use Thread qw/async yield/;

use threads qw/yield/;
use threads::shared;

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top