Help writing semaphore codes

Discussion in 'Perl Misc' started by Thierry, Jul 12, 2007.

  1. Thierry

    Thierry Guest

    I have the following perl file greet.pl

    # LOCK ACQUIRED
    print "hello";
    # LOCK RELEASED

    My linux os can have four threads running the greet.pl file
    simultaneously. How should I write the semaphore codes so that only
    one thread can access the print "hello" line at any given time?


    Thierry
     
    Thierry, Jul 12, 2007
    #1
    1. Advertising

  2. Thierry coughed up some electrons that declared:

    > I have the following perl file greet.pl
    >
    > # LOCK ACQUIRED
    > print "hello";
    > # LOCK RELEASED
    >
    > My linux os can have four threads running the greet.pl file
    > simultaneously. How should I write the semaphore codes so that only
    > one thread can access the print "hello" line at any given time?
    >
    >
    > Thierry


    Hi,

    In the context of cooperating forked processes (not threads as you
    mentioned), I used this module:

    IPC::Semaphore

    However, there might be a better way with threads - best hang around in case
    someone else can add anything. I never do threaded programming.

    Cheers

    Tim
     
    Tim Southerwood, Jul 12, 2007
    #2
    1. Advertising

  3. Thierry

    Guest

    Thierry <> wrote:
    > I have the following perl file greet.pl
    >
    > # LOCK ACQUIRED
    > print "hello";
    > # LOCK RELEASED
    >
    > My linux os can have four threads running the greet.pl file
    > simultaneously. How should I write the semaphore codes so that only
    > one thread can access the print "hello" line at any given time?


    You can declare a shared variable (see threads::shared) to serve as a
    sempahore, and lock that.

    {
    lock $sema;
    print "hello";
    } # automatically released on scope exit

    Or maybe use Thread::Semaphore, which I have never used.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jul 12, 2007
    #3
    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. Greg  --
    Replies:
    4
    Views:
    2,173
  2. Replies:
    2
    Views:
    2,848
    Malcolm
    Aug 20, 2005
  3. techi_C
    Replies:
    2
    Views:
    1,414
    Richard Bos
    Aug 10, 2006
  4. ricky
    Replies:
    2
    Views:
    784
    Nick Keighley
    Apr 5, 2007
  5. Allen
    Replies:
    1
    Views:
    659
    Mark Rae [MVP]
    Dec 3, 2007
Loading...

Share This Page