[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. Advertisements

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:
    703
    Andrew MacIntyre
    Dec 5, 2003
  2. yoda
    Replies:
    2
    Views:
    644
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    613
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,664
    Arne Vajhøj
    Apr 9, 2008
  5. k3xji
    Replies:
    7
    Views:
    1,045
    Gabriel Genellina
    Dec 30, 2008
  6. nano2k

    Application.Lock()/UnLock() or lock(Application)

    nano2k, Jul 23, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    473
    nano2k
    Aug 9, 2007
  7. Replies:
    2
    Views:
    186
  8. king
    Replies:
    5
    Views:
    342
Loading...