Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutex locks vs Threading locks. Which to use?

My main question is does the Threading lock object create atomic locks? It doesn't say that the lock is atomic in the module documentation. in pythons mutex documentation it does say the mutex lock is atomic but it seems that I read somewhere that in fact it isn't. I am wondering if someone could could give me a bit of insight on this mater. Which lock should I use. I am currently running my scripts using python 2.4

like image 515
Richard Avatar asked Jul 23 '10 16:07

Richard


People also ask

When should a mutex be used?

Mutex: Use a mutex when you (thread) want to execute code that should not be executed by any other thread at the same time. Mutex 'down' happens in one thread and mutex 'up' must happen in the same thread later on.

What is the disadvantage of mutex lock?

The major drawback of a mutex lock is that it lets the thread spinlock if the lock is not available. While one thread has acquired the lock and is in its critical section, all other threads attempting to acquire the lock are in a loop where the thread periodically checks whether the lock is available.

Should I use mutex or semaphore?

The correct use of a semaphore is for signaling from one task to another. A mutex is meant to be taken and released, always in that order, by each task that uses the shared resource it protects. By contrast, tasks that use semaphores either signal or wait—not both.

Why are atomic variables more efficient than mutex locks?

atomic integer is a user mode object there for it's much more efficient than a mutex which runs in kernel mode. The scope of atomic integer is a single application while the scope of the mutex is for all running software on the machine.


1 Answers

Locks of any nature would be rather useless if they weren't atomic - the whole point of the lock is to allow for higher-level atomic operations.

All of threading's synchronization objects (locks, rlocks, semaphores, boundedsemaphores) utilize atomic instructions, as do mutexes.

You should use threading, since mutex is actually deprecated going forward (and removed in Python 3).

like image 50
Amber Avatar answered Oct 19 '22 16:10

Amber