Re: Approaches to concurrency
by BrowserUk (Patriarch) on Feb 09, 2009 at 11:35 UTC
|
But in perl there are Threads, ithreads, threads, 5005threads
There are only ithreads!
- "Threads" and "5005threads" were the same, and are long since gone.
Ignore these and the module that implemented them: Threads.
- "threads" and "ithreads" are the same thing.
- "ithreads" means interpreter threads.
That is, each system thread runs an independant copy of the interpreter, with all non-explicitly shared variables created by that thread, being confined to that interpreter.
Ie. If you don't explicitly share it, each thread runs (almost) as a separate process would.
- "threads" is the name of the module threads that implements ithreads.
To share data between threads, you must use threads::shared also.
The are some other ways also, but enough for now.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
How about POE?
by denny (Novice) on Feb 09, 2009 at 12:54 UTC
|
I'm not sure what you're trying to do, but I've used POE a few times and found it fairly easy to work with:
http://poe.perl.org/?What_POE_Is
"POE provides medium- and low-level concurrency functions. Components use them to perform their tasks. The functions are also available to programmers who prefer to avoid the overhead of components at the expense of writing more code."
| [reply] |
Re: Approaches to concurrency
by JavaFan (Canon) on Feb 09, 2009 at 11:54 UTC
|
But sometimes I want shared access to my data, so I've looked at threads.
Note that you can still share data if you fork() instead of using threads by using shared memory (well, at least on Unix systems you can). While that is certainly more icky to work with that shared data using threads, threads come with a performance penalty that is severe enough perl doesn't compile with threads enabled unless specifically asked to do so. (It's a penalty paid even if you don't use multiple threads).
| [reply] |
|
|
| [reply] |
|
|
Let me give just one datapoint:
#!/usr/bin/perl
use strict;
use warnings;
undef $/;
my $code = <DATA>;
my $RUNS = 10;
my $no_threads = "/opt/perl/bin/perl-64";
my $threads = "/opt/perl/bin/thr-perl-64";
foreach my $perl ($no_threads, $threads) {
my ($sum_r, $sum_u) = (0, 0);
foreach my $runs (0 .. $RUNS) {
my $res = `/usr/bin/time -p $perl -e '$code' 2>&1 `;
next unless $runs;
$sum_r += ($res =~ /real\s*(\d+\.\d+)/)[0];
$sum_u += ($res =~ /user\s*(\d+\.\d+)/)[0];
}
printf "real: %.2f; user: %.2f\n", $sum_r / $RUNS, $sum_u / $RUNS;
}
__DATA__
use strict;
use warnings;
foreach (1 .. 1000) {
my @arr;
foreach (1 .. 1000) {
push @arr, 1;
}
}
__END__
real: 0.43; user: 0.43
real: 0.54; user: 0.54
The short program, pushing '1' a thousand time on an array, takes about 25% more when threads are compiled in. Of course, YMMV. | [reply] [d/l] |
|
|
|
|
Last time I ran it through perlbench, the difference was about 15%. Nevertheless, most Linux distros come with a threaded perl these days.
| [reply] |
|
|
|
|
|
|
Re: Approaches to concurrency
by rhesa (Vicar) on Feb 09, 2009 at 15:39 UTC
|
I think the forks module deserves a mention here. It's a "drop-in replacement for Perl threads using fork()". | [reply] |
|
|
...and is slower, more cpu intensive, more memory hungry and threads.
Shared variables are "achieved" by serialising them with Storable and transmitting them to all the other threads</strike</> processes via sockets.
That's like putting a single lane chicane on a multi-lane highway. Necessary occasionally, but totally defeating the purpose of having multiple lanes.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
|
|
But it'd work on non-threaded perls, and that's the use case for it. It certainly wouldn't make sense to replace actual threads with forks, especially on windows :)
| [reply] |