[#3006] mismatched quotation — "stevan apter" <apter@...>

ruby documentation uses a punctuation convention i've never seen

13 messages 2000/05/27

[ruby-talk:02927] Re: mutex mechanism?

From: kjana@... (YANAGAWA Kazuhisa)
Date: 2000-05-22 13:08:49 UTC
List: ruby-talk #2927
In message <200005212300.TAA27003@toolshed.com>
Andy@Toolshed.Com writes:

> Can someone please educate me on the preffered method of handling
> mutual exclusion in threads?  It would seem that Thread.critical isn't
> quite sufficient, and their exists a number of library modules that
> provide some form of mutex or other synchronization mechanism.
> 
> Which one is the preferred method?

That's depends on the situation where synchronization is needed.


Thread.critical is the most primitive way of mutual exclusion.  You
can forbid another thread to run by `Thread.critical = true'.  However
this method is too primitive to use everywhere.  We almost always want
to exclude another thread from some critical region of code.  It is
undesirable that a thread which does not touch critical resources also
stopped unnecessarily.


Mutex provides more loose and desirable functions to implement
critical region.  Threads that use shared resources acquire the Mutex
attached the resources.  Since Mutex can be acquired by just one
thread at a time, only the thread got a Mutex can do something with
the resources.  At here, threads which does not use the resources and
not acquire the Mutex can run in parallel.


Monitor is an abstract that represents a resource which can be
accessed by only one thread at a time.  Basically Monitor can be seen
as an object which methods are all in critical region attached to one
Mutex --- a thread executes a method of Monitor, all other thread call
a method of the Monitor is awaited.

The thread in Monitor can be wait for some conditions are met on
ConditionVariable.  When the thread calls ConditionVarible#wait or
like, the thread sleeps and release the Monitor for another thread to
progress.  When a thread calls ConditionVariable#signal or like, the
thread awaited on the ConditionVariable acquire the Monitor again and
go on.  These scheduling and acquire/release are done automatically.
So more easy to read/write when complex behavior is needed.


Sync resembles to Mutex, both are used to mutual exclusion, but Sync
is differ from Mutex because it provides a facility of `reader/writer
lock'.  In threaded applications we often need maximum parallelism.
If the shared resources are frequently read but modified not so often,
`reader' threads can be parallelly executed without a significant
conflict.  Sync is designed for these situations.


# Well, long but a not so useful answer.  *sigh*

-- 
kjana@os.xaxon.ne.jp                                   May 22, 2000
Of two evils choose the lesser.

In This Thread

Prev Next