[threads] problem with subroutine lock

Discussion in 'Perl Misc' started by Michael Schmitt, Dec 20, 2004.

  1. 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?
     
    Michael Schmitt, Dec 20, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    3
    Views:
    508
    Andrew MacIntyre
    Dec 5, 2003
  2. Robert Brewer
    Replies:
    0
    Views:
    500
    Robert Brewer
    Dec 5, 2003
  3. k3xji
    Replies:
    7
    Views:
    844
    Gabriel Genellina
    Dec 30, 2008
  4. Replies:
    2
    Views:
    136
  5. king
    Replies:
    5
    Views:
    202
Loading...

Share This Page