Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help writing semaphore codes

Reply
Thread Tools

Help writing semaphore codes

 
 
Thierry
Guest
Posts: n/a
 
      07-12-2007
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

 
Reply With Quote
 
 
 
 
Tim Southerwood
Guest
Posts: n/a
 
      07-12-2007
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
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-12-2007
Thierry <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write codes to connect access database using html codes hiralcp Software 0 12-18-2007 08:49 PM
How to use file I/o codes with form and controls codes Allen ASP .Net 1 12-03-2007 12:04 AM
Help Needed : Race condition while removing semaphore(sysvsem semaphore) techi_C C Programming 2 08-10-2006 12:27 PM
Virtual Key Codes, Scan Codes and ASCII Codes in C gj_williams2000@yahoo.co.uk C Programming 2 08-20-2005 11:04 AM
RegEx replace of html codes to ascii codes Greg -- ASP .Net 4 08-09-2005 07:27 PM



Advertisments