Discussion:
Lock vs Mutex
Owen Durni
2009-12-24 00:11:49 UTC
Permalink
Besides the differences in API, is there any difference between a
[neko|cpp].vm.Lock and a [neko|cpp].vm.Mutex?

Owen
--
haXe - an open source web programming language
http://haxe.org
Nicolas Cannasse
2009-12-24 10:17:26 UTC
Permalink
Post by Owen Durni
Besides the differences in API, is there any difference between a
[neko|cpp].vm.Lock and a [neko|cpp].vm.Mutex?
A lock is much more feature-complete, and can be released by an
different thread than the one which acquired it. You can also do several
release() and as much wait() if you want for example limit a resource to
a given number of users.

But Lock is slower than Mutex, especially on Windows.

Nicolas
--
haXe - an open source web programming language
http://haxe.org
Owen Durni
2009-12-24 16:39:21 UTC
Permalink
On Thu, Dec 24, 2009 at 5:17 AM, Nicolas Cannasse
Post by Owen Durni
Besides the differences in API, is there any difference between a
[neko|cpp].vm.Lock and a [neko|cpp].vm.Mutex?
A lock is much more feature-complete, and can be released by an different
thread than the one which acquired it.
I assume this is not the case with a Mutex then (this should probably
be mentioned in the API).
You can also do several release() and
as much wait() if you want for example limit a resource to a given number of
users.
But Lock is slower than Mutex, especially on Windows.
So it sounds like one should use a Mutex unless the additional
features of a Lock are required.
Hi Owen,
A lock allows you to wait until all the locks have been released.
A mutex will let you lock, check a variable, and unlock, but you
will need to do this in a loop, which wastes CPU.
This is inconsistent with the docs for Mutex: "The calling thread
locks the mutex. If another thread has already locked the mutex, the
calling thread will block until it is released." (unless Mutex
"blocks" by spinning, in which case the docs should say so)

In any case, I just decided to experiment with synchronous message
passing in haXe. Probably won't have a chance to test this until after
the holidays, but my first step was to take the basic locks provided
and create a barrier:
http://www.evaryu.com/hg/concurrent/file/tip/src/concurrent/primatives/Barrier.hx

I haven't even had time to run the code yet, so I'm sure it has bugs still :)

Owen
--
haXe - an open source web programming language
http://haxe.org
Hugh Sanderson
2009-12-24 11:11:59 UTC
Permalink
Hi Owen,
A lock allows you to wait until all the locks have been released.
A mutex will let you lock, check a variable, and unlock, but you
will need to do this in a loop, which wastes CPU.

Typically, you might have a lock that checks to see if an message queue
is empty. The calling process will block on the lock, waiting for
something to arrive. Sometime later, a client will add something
to the queue, and release a lock at the same time, freeing the processing
thread.

Hugh
Post by Owen Durni
Besides the differences in API, is there any difference between a
[neko|cpp].vm.Lock and a [neko|cpp].vm.Mutex?
Owen
--
haXe - an open source web programming language
http://haxe.org
Loading...