Help writing semaphore codes

T

Thierry

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
 
T

Tim Southerwood

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
 
X

xhoster

Thierry said:
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
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top