[threads] problem with subroutine lock

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top