Re: Perl threads



ivakras1@xxxxxxxxx wrote:
Hi all!
I have a perl threads question. Of cuz i have read perl threads
tutorial and think its clear to me. But some questions...
Q1: Is threads->create method only creates thread, but not runs theads
until im join it (or detach)?
The folowing code and its output makes me think so:
=====================
...


Using strict is probably even more important under threads than it
is without threads. You should use strict.



for (my $i=0;$i<=10;$i++)
{
my $trl=threads->create(\&my_funct,$my_args);
if (@tc=threads->list){print "\t".@tc." threads running";}
}
....
if (@tc=threads->list)
{
print "\nClosing threads: ";
while (@tc-1)
{
@tc=threads->list;
print @tc." ";
threads->list->join;
}
}
else
{
exit(0);
}

...
sub my_funct
{
$id=int(rand(1000));
setlogsock('unix'); #im using syslog to write a log file in /
var/log/messages

Instead of putting in a comment telling us you are using syslog, you
should show us the code that actually "use"s syslog. (By which
you presumably mean Sys::Syslog?) Is Sys::Syslog thread safe?


Coments: i create 11 threads first, and than join them. Each thread
waits for 0-5 (rand(5)) seconds, and than exits, writes a start time
and a stop time in /var/log/messages via syslogd. It looks like thread-
create only creates the thread, not runs it. The output shoes it:
threads->list increments every threads->create, but some of created
threads would be done during this time,

The criteria for a thread to show up in list is not that it be "running"

From the doc:
threads->list();
This will return a list of all non joined, non
detached threads.

So threads that are "finished" but still haven't been joined or detached
will be in the list.


if i wrong!!! When i threads-
list->join one by one the threads seems to start, and threads->list
starts decreasing! Can anybody answer me?

threads->list doesn't do what you think it does.



Q2: threads->list returns a list of unjoind or undetached threads.

Er, OK, so you do know this. Then why the confusion earlier?

How
to return a count of all threads, joined and detached both?? Then the
main program exits while some child threads are still running
(detached), perl warns me the count of running parentless threads. How
can i get that count during main program still alive?!

The easy way is not to detach the threads. Then, just before the parent
exits, run through threads->list and join all of them. Or you could use
Thread::Running

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.