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

W

web.dizign

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!
 
B

Brian Candler

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"
 
E

Emilio Tagua

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
 
M

MenTaLguY

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
 
W

web.dizign

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!
 

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

Forum statistics

Threads
474,241
Messages
2,571,222
Members
47,856
Latest member
mmorais

Latest Threads

Top