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

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!

On Tue, May 01, 2007 at 01:45:10PM +0900, [email protected] 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_threads.html

There’s a section headed “Mutual Exclusion”

On 5/1/07, [email protected] [email protected] 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_exclusion

Easy examples @: http://www.rubycentral.com/book/tut_threads.html

On May 1, 4:58 pm, MenTaLguY [email protected] wrote:

Hi, Mutex comes for Mutual Exclusion. This means that 2 or more
-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!

On Wed, 2 May 2007 05:27:18 +0900, “Emilio T.” [email protected]
wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs