I
ivakras1
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:
=====================
....
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
openlog('PerlSniffer','','debug'); #open a log
syslog('info', "Thread $id start"); #WRITE THREAD START TIME
$w=int(rand(5));
sleep($w); #SLEEP $w SECONDS
syslog('info', "Thread $id stop after $w s sleep"); #WRITE
THREAD STOP TIME
closelog;
return(1);
}
.....
=======================
The output of that program:
0 threads running
1 threads running
2 threads running
3 threads running
4 threads running
5 threads running
6 threads running
7 threads running
8 threads running
9 threads running
10 threads running
Closing threads: 10 9 8 7 6 5 4 3 2 1 0
=======================
/var/log/messages for that:
Jul 30 12:20:43 test_comp PerlSniffer: Thread 914 start
Jul 30 12:20:44 test_comp PerlSniffer: Thread 560 start
Jul 30 12:20:45 test_comp PerlSniffer: Thread 914 stop after 2 s sleep
Jul 30 12:20:45 test_comp PerlSniffer: Thread 560 stop after 1 s sleep
Jul 30 12:20:46 test_comp PerlSniffer: Thread 451 start
Jul 30 12:20:46 test_comp PerlSniffer: Thread 451 stop after 0 s sleep
Jul 30 12:20:48 test_comp PerlSniffer: Thread 303 start
Jul 30 12:20:48 test_comp PerlSniffer: Thread 303 stop after 0 s sleep
Jul 30 12:20:49 test_comp PerlSniffer: Thread 834 start
Jul 30 12:20:50 test_comp PerlSniffer: Thread 311 start
Jul 30 12:20:51 test_comp PerlSniffer: Thread 311 stop after 1 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 677 start
Jul 30 12:20:52 test_comp PerlSniffer: Thread 677 stop after 0 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 834 stop after 3 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 133 start
Jul 30 12:20:52 test_comp PerlSniffer: Thread 133 stop after 0 s sleep
Jul 30 12:20:54 test_comp PerlSniffer: Thread 718 start
Jul 30 12:20:55 test_comp PerlSniffer: Thread 718 stop after 0 s sleep
Jul 30 12:20:56 test_comp PerlSniffer: Thread 909 start
Jul 30 12:20:58 test_comp PerlSniffer: Thread 520 start
Jul 30 12:21:00 test_comp PerlSniffer: Thread 909 stop after 4 s sleep
Jul 30 12:21:02 test_comp PerlSniffer: Thread 520 stop after 4 s sleep
=======================
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-
threads would be done during this time, if i wrong!!! When i threads-
Q2: threads->list returns a list of unjoind or undetached threads. 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?!
Thanks a lot and forgive me my baaad english, im russian actually.
Praying for the answers! Thanks again!
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:
=====================
....
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
openlog('PerlSniffer','','debug'); #open a log
syslog('info', "Thread $id start"); #WRITE THREAD START TIME
$w=int(rand(5));
sleep($w); #SLEEP $w SECONDS
syslog('info', "Thread $id stop after $w s sleep"); #WRITE
THREAD STOP TIME
closelog;
return(1);
}
.....
=======================
The output of that program:
0 threads running
1 threads running
2 threads running
3 threads running
4 threads running
5 threads running
6 threads running
7 threads running
8 threads running
9 threads running
10 threads running
Closing threads: 10 9 8 7 6 5 4 3 2 1 0
=======================
/var/log/messages for that:
Jul 30 12:20:43 test_comp PerlSniffer: Thread 914 start
Jul 30 12:20:44 test_comp PerlSniffer: Thread 560 start
Jul 30 12:20:45 test_comp PerlSniffer: Thread 914 stop after 2 s sleep
Jul 30 12:20:45 test_comp PerlSniffer: Thread 560 stop after 1 s sleep
Jul 30 12:20:46 test_comp PerlSniffer: Thread 451 start
Jul 30 12:20:46 test_comp PerlSniffer: Thread 451 stop after 0 s sleep
Jul 30 12:20:48 test_comp PerlSniffer: Thread 303 start
Jul 30 12:20:48 test_comp PerlSniffer: Thread 303 stop after 0 s sleep
Jul 30 12:20:49 test_comp PerlSniffer: Thread 834 start
Jul 30 12:20:50 test_comp PerlSniffer: Thread 311 start
Jul 30 12:20:51 test_comp PerlSniffer: Thread 311 stop after 1 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 677 start
Jul 30 12:20:52 test_comp PerlSniffer: Thread 677 stop after 0 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 834 stop after 3 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 133 start
Jul 30 12:20:52 test_comp PerlSniffer: Thread 133 stop after 0 s sleep
Jul 30 12:20:54 test_comp PerlSniffer: Thread 718 start
Jul 30 12:20:55 test_comp PerlSniffer: Thread 718 stop after 0 s sleep
Jul 30 12:20:56 test_comp PerlSniffer: Thread 909 start
Jul 30 12:20:58 test_comp PerlSniffer: Thread 520 start
Jul 30 12:21:00 test_comp PerlSniffer: Thread 909 stop after 4 s sleep
Jul 30 12:21:02 test_comp PerlSniffer: Thread 520 stop after 4 s sleep
=======================
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-
threads->list increments every threads->create, but some of createdcreate only creates the thread, not runs it. The output shoes it:
threads would be done during this time, if i wrong!!! When i threads-
starts decreasing! Can anybody answer me?list->join one by one the threads seems to start, and threads->list
Q2: threads->list returns a list of unjoind or undetached threads. 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?!
Thanks a lot and forgive me my baaad english, im russian actually.
Praying for the answers! Thanks again!