Click here to get back home

Threading NOT working as expected

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Threading NOT working as expected Ted 02-25-2008
Posted by Ted on February 25, 2008, 11:36 am
Please log in for more thread options
When I first tried creating perl threads, the main process ended after
the threads where created but before any of them really started. On
reading further, I saw that I had to join the threads so that the main
process would sit idle waiting for the threads to finish. So I added
statements to join each thread. But now, it looks like the
consequence of this is that the code in each thread is executed one
after the other, as if it was a single process rather than a set of
independantly executing threads. I had thought of joining only the
last created thread, but there is no guarantee that the last thread
will take the longest time to complete. So how do I create these
threads and guarantee that they will execute in parallel, and that the
main process will wait idle until all have finished? I am trying to
use a script to manage this analysis since there may be, in any given
batch, several dozen SQL scripts that need to be executed (each is
independant, of course, with no possibility of interacting with the
others), and I want to run these scripts by invoking a single perl
script that allows them to run in parallel making full use of all the
available computing resources.

Thanks

Ted

Posted by Joost Diepenmaat on February 25, 2008, 11:51 am
Please log in for more thread options

> When I first tried creating perl threads, the main process ended after
> the threads where created but before any of them really started. On
> reading further, I saw that I had to join the threads so that the main
> process would sit idle waiting for the threads to finish. So I added
> statements to join each thread. But now, it looks like the
> consequence of this is that the code in each thread is executed one
> after the other, as if it was a single process rather than a set of
> independantly executing threads.

It shouldn't.

#!/usr/local/bin/perl -w
use strict;
use threads;

my @thrds = map { my $i = $_; threads->new(sub {
print "started $i\n";
sleep 2;
print "stopped $i\n" } ) } 0 .. 10;

$_->join for @thrds;

output:
started 0
started 1
started 2
started 3
started 4
started 5
started 6
started 7
started 8
started 9
started 10
stopped 0
stopped 1
stopped 2
stopped 3
stopped 4
stopped 5
stopped 6
stopped 7
stopped 8
stopped 9
stopped 10



--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by xhoster on February 25, 2008, 12:30 pm
Please log in for more thread options
> When I first tried creating perl threads, the main process ended after
> the threads where created but before any of them really started. On
> reading further, I saw that I had to join the threads so that the main
> process would sit idle waiting for the threads to finish. So I added
> statements to join each thread. But now, it looks like the
> consequence of this is that the code in each thread is executed one
> after the other, as if it was a single process rather than a set of
> independantly executing threads.

The problem is in line 42.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Ted on February 25, 2008, 1:18 pm
Please log in for more thread options
On Feb 25, 12:30=A0pm, xhos...@gmail.com wrote:
> > When I first tried creating perl threads, the main process ended after
> > the threads where created but before any of them really started. =A0On
> > reading further, I saw that I had to join the threads so that the main
> > process would sit idle waiting for the threads to finish. =A0So I added
> > statements to join each thread. =A0But now, it looks like the
> > consequence of this is that the code in each thread is executed one
> > after the other, as if it was a single process rather than a set of
> > independantly executing threads.
>
> The problem is in line 42.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate=

> this fact.

Will calling 'system' within a thread block all other threads in the
process until it has returned? If so, then that may be where my
problem lies?

If not, then I am puzzled.

Thanks

Ted

Posted by Willem on February 25, 2008, 2:11 pm
Please log in for more thread options
Ted wrote:
) When I first tried creating perl threads, the main process ended after
) the threads where created but before any of them really started. On
) reading further, I saw that I had to join the threads so that the main
) process would sit idle waiting for the threads to finish. So I added
) statements to join each thread. But now, it looks like the
) consequence of this is that the code in each thread is executed one
) after the other, as if it was a single process rather than a set of
) independantly executing threads.

How do you know this ? Have you tested this thoroughly ?

Note that if you run multiple threads, that one thread will be running
at a time, and the OS will switch to the next thread every so often.

So if you do a trivial task in one thread, then, yes, it's very likely that
the operating system won't have a chance to switch to other tasks before it
completes, effectively completing one task after another.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Similar ThreadsPosted
Multi-Threading May 25, 2005, 2:57 am
Threading - share August 27, 2006, 3:15 pm
Re: Allowing threading with CGI August 27, 2006, 10:36 pm
Re: Allowing threading with CGI August 28, 2006, 12:08 pm
Allowing threading with CGI August 27, 2006, 9:38 pm
Sockets, gui, threading questions December 1, 2004, 10:34 pm
Threading and production environments February 3, 2006, 2:05 pm
Noob threading question July 25, 2006, 9:34 am
perl_parse and multi threading August 1, 2006, 9:09 am
Is this expected in a foreach()? May 8, 2008, 9:47 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap