M
Michael Schmitt
I want a subroutine Lock. I tried use attrs qw(locked); but i didnt
work, all thread run into the sub. Now I tried to use semaphores
instead like this;
#!/usr/bin/perl -w
use Thread;
use Thread::Semaphore;
$sem = Thread::Semaphore->new();
sub dprint {
my $param = shift;
$sem->down;
print("Thread $param started\n");
sleep(5);
print("Thread $param ends\n");
$sem->up;
}
$thread1 = new Thread \&dprint, t1;
$thread2 = new Thread \&dprint, t2;
$thread1->join();
$thread2->join();
exit();
I want
thread1 started
thread1 ends
thread2 started
thread2 ends
But i alway get
thread1 started
thread2 started
thread1 ends
thread2 ends
What is wrong here?
work, all thread run into the sub. Now I tried to use semaphores
instead like this;
#!/usr/bin/perl -w
use Thread;
use Thread::Semaphore;
$sem = Thread::Semaphore->new();
sub dprint {
my $param = shift;
$sem->down;
print("Thread $param started\n");
sleep(5);
print("Thread $param ends\n");
$sem->up;
}
$thread1 = new Thread \&dprint, t1;
$thread2 = new Thread \&dprint, t2;
$thread1->join();
$thread2->join();
exit();
I want
thread1 started
thread1 ends
thread2 started
thread2 ends
But i alway get
thread1 started
thread2 started
thread1 ends
thread2 ends
What is wrong here?