You could try either queues or semaphores:
 
1) Create a new Thread::Queue and have each thread block waiting to pop
something off the queue - create all your threads, then throw as many
1's onto the queue as you need - the threads will all start going within
a few processor quantums of each other.
 
2) Create x Thread::Semaphores, have each thread block, then drop them
all after the threads are created.
 
E.g. (totally untested and not pretty but you get the idea):
 
my $threads = 9;
# Spawn threads
for (1..$threads) {
    my $sem   = new Thread::Semaphore(1);
    my $thread = new threads (\&bla, $sem);
    push @threads, [$thread, $sem];
}
# Now threads are all waiting, raise sems so they can start
foreach (@threads) {
        my $sem = $_->[1];
        $sem->up;
}
# All threads running now - next routine will block until they exit
foreach (@threads) {
        my $thread = $_->[0];
        $thread->join;
}

sub bla {
        my $sem = shift;
        $sem->down;
        # do stuff
}

________________________________

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Plymouth Rock
Sent: 13 April 2005 05:27
To: perl-win32-users@listserv.ActiveState.com
Subject: Threads Synchronization


 My script uses threads module for creation several threads. Each thread
prints
certain letter for several times via some pause. Kind of letter, number
of times,
length of pause are different for every thread. These threads should be
work in
parallel. Also, I have to synchronize all starts of the threads, that is
all 9
children should be start at the same time, almost 'simultaneously' (at
least for
eyes) , without visual delays between each other. Still I've too large
total
delay between 'a'-letter and 'i'-letter: about 9 secs on Pentium 133.
I've tried:
Thread::Barrier - thread execution barrier/
Win32::IPC - Base class for Win32 synchronization objects/
Proc-SyncExec-1.01 (non-downloadable from CPAN with 'ppm install'),
but nothing could affect to the starts synchronization. Also I'd trying
to use
locks, semaphores, alarms, cond_wait, cond_signal, cond_broadcast...
without a
success. Maybe, Thread::Signal?.. IPC's kill()?..
 This script is CGI, for win32, IE6; it uses buffering $|++, which may
sometimes
be a cause of certain visual problems in browser. Anyway, you can run it
just in
a shell and look at process of printing. It's most likely, just after
start and
HTML-dumping you won't see: abcdefghi (etc in non-regulated order). It
will be
something like this: ababcbacd (etc in non-regulated order). As I
understand,
such the asynchronous starts do happen due to sequential subN's works
out
without waiting of preparing all future threads to run, every time once
each
subN being called in the code:
my $thrN = threads->new(\&subN);
 So, how to sync all the threads starts (or just all letters printing's
start)?
 

----------------------------------------------------------
                    Source Code:
----------------------------------------------------------
 
#!C:\Perl\bin\perl -w
 
$|++;
 
print "Content-type: text/html\n\n";
 
use warnings;
 
use Time::HiRes;
use threads;
# use threads::shared;
# use Thread::Barrier;
# use Thread::Semaphore;
 
$doc_top = "<html>\n";
$doc_top .= "<head>\n";
$doc_top .= "<script>\n";
$doc_top .= "function set(id,text) {\n";
$doc_top .= " document.getElementById(id).innerText = text\n";
$doc_top .= "}\n";
$doc_top .= "</script>\n\n";
$doc_top .= "</head>\n";
$doc_top .= "<body>\n\n";
$doc_top .= "<table style=\"font: 8pt Verdana, Arial, Helvetica,
Sans-serif; line-height:8pt;\" cellSpacing=\"1\" cellPadding=\"2\"
width=\"21%\" border=\"1\">\n";
$doc_top .= "\t<tr>\n";
$doc_top .= "\t\t<td>\n";
 
$doc_middle ='';
 
$doc_bottom .= "\t\t</td>\n";
$doc_bottom .= "\t</tr>\n";
$doc_bottom .= "</table>\n\n";
 
for ($j = 0; $j <= 12; $j++) {
  $doc_middle .= "\t\t\t<tr>\n";
  for ($i = 1; $i <= 9; $i++) {
    $doc_middle .= "\t\t\t\t<td width=\"10%\" id='cell$i$j'
bgColor=\"#eeeeee\" align=\"center\">&nbsp</td>\n";
  }
  $doc_middle .= "\t\t\t</tr>\n";
}
 
print $doc_top.$doc_middle.$doc_bottom;
print "<font style=\"font: 8pt Verdana, Arial, Helvetica, Sans-serif;
line-height:8pt;\">";
 
sub sub1 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 8; $cur_var++) {
    print "<script>set('cell1$cur_var', 'a')</script>\n";
    Time::HiRes::sleep(0.4);
  }
}
sub sub2 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 5; $cur_var++) {
    print "<script>set('cell2$cur_var', 'b')</script>\n";
    Time::HiRes::sleep(0.2);
  }
}
sub sub3 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 11; $cur_var++) {
    print "<script>set('cell3$cur_var', 'c')</script>\n";
    Time::HiRes::sleep(0.3);
  }
}
sub sub4 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 6; $cur_var++) {
    print "<script>set('cell4$cur_var', 'd')</script>\n";
    Time::HiRes::sleep(1.1);
  }
}
sub sub5 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 2; $cur_var++) {
    print "<script>set('cell5$cur_var', 'e')</script>\n";
    Time::HiRes::sleep(0.5);
  }
}
sub sub6 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 9; $cur_var++) {
    print "<script>set('cell6$cur_var', 'f')</script>\n";
    Time::HiRes::sleep(0.6);
  }
}
sub sub7 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 3; $cur_var++) {
    print "<script>set('cell7$cur_var', 'g')</script>\n";
    Time::HiRes::sleep(0.7);
  }
}
sub sub8 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 5; $cur_var++) {
    print "<script>set('cell8$cur_var', 'h')</script>\n";
    Time::HiRes::sleep(0.8);
  }
}
sub sub9 {
  my $cur_var = 0;
  for($cur_var = $cur_var; $cur_var <= 6; $cur_var++) {
    print "<script>set('cell9$cur_var', 'i')</script>\n";
    Time::HiRes::sleep(0.9);
  }
}
 
my $thr1 = threads->new(\&sub1);
my $thr2 = threads->new(\&sub2);
my $thr3 = threads->new(\&sub3);
my $thr4 = threads->new(\&sub4);
my $thr5 = threads->new(\&sub5);
my $thr6 = threads->new(\&sub6);
my $thr7 = threads->new(\&sub7);
my $thr8 = threads->new(\&sub8);
my $thr9 = threads->new(\&sub9);
 
$thr1->join;
$thr2->join;
$thr3->join;
$thr4->join;
$thr5->join;
$thr6->join;
$thr7->join;
$thr8->join;
$thr9->join;
 
----------------------------------------------------------
                    Results:
----------------------------------------------------------
 
a b c d e f g h i 
a b c d e f g h i 
a b c d e f g h i 
a b c d   f g h i 
a b c d   f   h i 
a b c d   f   h i 
a   c d   f     i 
a   c     f       
a   c     f       
    c     f       
    c             
    c             
                  
----------------------------------------------------------
 
 Thanx.

*****************************************************************
Gloucester Research Limited believes the information 
provided herein is reliable. While every care has been 
taken to ensure accuracy, the information is furnished 
to the recipients with no warranty as to the completeness 
and accuracy of its contents and on condition that any 
errors or omissions shall not be made the basis for any 
claim, demand or cause for action.
*****************************************************************


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to