Home: Perl Programming Help: Advanced:
Mutex in perl



harinarayana
New User

Dec 17, 2013, 4:38 AM


Views: 19021
Mutex in perl

I want to lock some code in perl script using mutex semaphore,Could you please help with some example how to initialize,lock and unlock the mutex.


FishMonger
Veteran / Moderator

Dec 17, 2013, 6:13 AM


Views: 19014
Re: [harinarayana] Mutex in perl

What do you mean by "lock some code in perl script"?

have you looked at any of the mutex or semaphore related modules on cpan?
http://search.cpan.org/search?query=mutex&mode=all
http://search.cpan.org/search?query=semaphore&mode=all


harinarayana
New User

Dec 18, 2013, 6:22 AM


Views: 18905
Re: [FishMonger] Mutex in perl

Actually I have a Perl script,In that i need to run some part of the code by single user at a time.Please see the example below.

Eg:
code...
.......
MUTEX LOCK
performTask();
MUTEX UNLOCK.
......
..........


(This post was edited by FishMonger on Dec 18, 2013, 6:51 AM)


FishMonger
Veteran / Moderator

Dec 18, 2013, 6:52 AM


Views: 18897
Re: [harinarayana] Mutex in perl

You need to provide more details on what your script is doing, how it's run and what the performTask() sub does.


FishMonger
Veteran / Moderator

Dec 18, 2013, 7:51 AM


Views: 18890
Re: [harinarayana] Mutex in perl

Based on the limited info you've given so far, my suggestion would be to modify the performTask() sub. The first thing it needs to do is check if a lock file (i.e. semaphore/mutex file) is already created. If it is, either exit or wait depending on your needs. If it hasn't been created, then the sub will create it and continue doing its work. The last thing the sub should do is remove the lock file so that the next process can do its work.