[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What's a Mutex and how do I use it?

web.dizign

5/1/2007 4:43:00 AM

Mutex seems very powerful and something I could use eventually as I
better my understanding of ruby. Thus far, I haven't found the
documentation verbose enough for me to grasp the concept of it. Could
anyone try their hand at writing a piece (or a reply) that would help
totally demistify this thing called a Mutex? Maybe a simple use case
scenario or maybe just 2 or 3 different ways of using it... that would
be enough for me to understand. Thanks!

4 Answers

Brian Candler

5/1/2007 7:56:00 AM

0

On Tue, May 01, 2007 at 01:45:10PM +0900, web.dizign@gmail.com wrote:
> Mutex seems very powerful and something I could use eventually as I
> better my understanding of ruby. Thus far, I haven't found the
> documentation verbose enough for me to grasp the concept of it. Could
> anyone try their hand at writing a piece (or a reply) that would help
> totally demistify this thing called a Mutex? Maybe a simple use case
> scenario or maybe just 2 or 3 different ways of using it... that would
> be enough for me to understand. Thanks!

http://www.rubycentral.com/book/tut_th...

There's a section headed "Mutual Exclusion"

Emilio Tagua

5/1/2007 8:27:00 PM

0

On 5/1/07, web.dizign@gmail.com <web.dizign@gmail.com> wrote:
> Mutex seems very powerful and something I could use eventually as I
> better my understanding of ruby. Thus far, I haven't found the
> documentation verbose enough for me to grasp the concept of it. Could
> anyone try their hand at writing a piece (or a reply) that would help
> totally demistify this thing called a Mutex? Maybe a simple use case
> scenario or maybe just 2 or 3 different ways of using it... that would
> be enough for me to understand. Thanks!
>

Hi, Mutex comes for Mutual Exclusion. This means that 2 or more
elements (process, threads, whatever) is trying to take the control or
use a resource, and this may cause an error because its a called
"critical section".

So the class Mutex gives you the lock for you to protect that resource
when doing concurrent programming.

Hope you understand me, im not the greatest teacher.

More information @: http://en.wikipedia.org/wiki/Mutual...

Easy examples @: http://www.rubycentral.com/book/tut_th...

MenTaLguY

5/1/2007 8:59:00 PM

0

On Wed, 2 May 2007 05:27:18 +0900, "Emilio Tagua" <miloops@gmail.com> wrote:
> On 5/1/07, web.dizign@gmail.com <web.dizign@gmail.com> wrote:
>> Mutex seems very powerful and something I could use eventually as I
>> better my understanding of ruby. Thus far, I haven't found the
>> documentation verbose enough for me to grasp the concept of it. Could
>> anyone try their hand at writing a piece (or a reply) that would help
>> totally demistify this thing called a Mutex? Maybe a simple use case
>> scenario or maybe just 2 or 3 different ways of using it... that would
>> be enough for me to understand. Thanks!
>>
>
> Hi, Mutex comes for Mutual Exclusion. This means that 2 or more
> elements (process, threads, whatever) is trying to take the control or
> use a resource, and this may cause an error because its a called
> "critical section".

Specifically, things can get very confused if one thread accesses or modifies an object at the same time another thread is modifying it. To prevent such situations, a mutex can be employed.

A mutex is designed so that only one thread can "own" it at a time. If we associate a mutex with an object, and we ensure that each thread acquires the mutex when it starts using the object and releases it again when it is done, then we can guarantee that two threads never access the object at the same time, and problems are avoided.

However, rather than openly sharing objects between threads and protecting them with mutexes, it is often a better idea to communicate between threads using message queues. (the Queue class in thread.rb) You can put an object on a thread's queue when that thread needs to work with it, and it can then send any needed objects back using another queue when it is done.

-mental


web.dizign

5/2/2007 4:39:00 PM

0

On May 1, 4:58 pm, MenTaLguY <men...@rydia.net> wrote:
> On Wed, 2 May 2007 05:27:18 +0900, "Emilio Tagua" <milo...@gmail.com> wrote:
> > On 5/1/07, web.diz...@gmail.com <web.diz...@gmail.com> wrote:
> >> Mutex seems very powerful and something I could use eventually as I
> >> better my understanding of ruby. Thus far, I haven't found the
> >> documentation verbose enough for me to grasp the concept of it. Could
> >> anyone try their hand at writing a piece (or a reply) that would help
> >> totally demistify this thing called a Mutex? Maybe a simple use case
> >> scenario or maybe just 2 or 3 different ways of using it... that would
> >> be enough for me to understand. Thanks!
>
> > Hi, Mutex comes for Mutual Exclusion. This means that 2 or more
> > elements (process, threads, whatever) is trying to take the control or
> > use a resource, and this may cause an error because its a called
> > "critical section".
>
> Specifically, things can get very confused if one thread accesses or modifies an object at the same time another thread is modifying it. To prevent such situations, a mutex can be employed.
>
> A mutex is designed so that only one thread can "own" it at a time. If we associate a mutex with an object, and we ensure that each thread acquires the mutex when it starts using the object and releases it again when it is done, then we can guarantee that two threads never access the object at the same time, and problems are avoided.
>
> However, rather than openly sharing objects between threads and protecting them with mutexes, it is often a better idea to communicate between threads using message queues. (the Queue class in thread.rb) You can put an object on a thread's queue when that thread needs to work with it, and it can then send any needed objects back using another queue when it is done.
>
> -mental

Thank you all. And thanks Mental!

So I figure this is ideal if i'm fetching data from different sources
with different threads but writting them to the same a sqlite
database.... great!

And I must admit, I feel a little stupid now that I realize this pas
in the pickaxe, but thanks nevertheless!