how to enforce mutual exclusion in win32

C

chaojen.chen

Hi all,

I need to simultaneously create multiple processes running the same
Perl script in win32. Some part of the script needs mutual exclusion.
The Perl interpreter I use is ActivePerl. Does Perl provide anything in
win32 like Java's keyword, synchronized?

Thanks,

Chao-Jen
 
B

Ben Morrow

Quoth (e-mail address removed):
Hi all,

I need to simultaneously create multiple processes running the same
Perl script in win32. Some part of the script needs mutual exclusion.
The Perl interpreter I use is ActivePerl. Does Perl provide anything in
win32 like Java's keyword, synchronized?

sychronized in Java applies to threads, not processes. If you wish to
use threads in Perl (be warned that they are not as efficient as they
might be, but they can be useful on occasion, especially on Win32) first
make sure you are using at least 5.8.0 and then read the documentation
for the threads and threads::shared modules. You may want to start by
reading perldoc perlthrtut.

I thought perl had a :locked attribute on subroutines, but perldoc
attributes says it only worked for 5005threads (an older threading model
for Perl, which never came out of the experimental stage and has been
abandoned). You can place explicit locks using the threads::shared
mechanism, anyway.

If you do mean separate processes (and be aware that fork on Win32
actually creates threads, so if this is what you are doing you are
probably better off using threads explicitly), then the standard way of
synchronising is by using file locks. Create an empty file somewhere to
serve as a semaphore, and use flock to set locks on it as necessary. You
may need more than one file if your locking need to be finer-grained: be
aware that you must be *incredibly* careful in this sort of situation,
as there are many subtle pitfalls leading to very hard-to-find bugs.

There are also Win32-specific process synchronization primitives, but I
know nothing about them. If you are interested look on MSDN and at the
Win32::* modules on CPAN.

Ben
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top