Can Mutex be replaced with anything?


 
Thread Tools Search this Thread
Top Forums Programming Can Mutex be replaced with anything?
# 1  
Old 12-28-2009
Question Can Mutex be replaced with anything?

Hi All,

To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment.

-Thanks
# 2  
Old 12-28-2009
Try to search something about interlocked variables.

Thanks, kandrewo.
# 3  
Old 12-28-2009
Interlocked variables/operations are mostly a Microsoft win32 thing. Because some architectures capable of running Linux do not have the necessary hardware support, interlocked variables/operations are not available in Linux user space. They are available only in the kernel and only on some architectures.
# 4  
Old 12-28-2009
Mutexes, semaphores, and atomic operations have been around for decades, have been used by untold numbers of programmers, and have been examined, re-examined, and otherwise worked on by probably thousands of computer scientists and programmers of which probably more than a few have done Nobel-prize level work in science and/or mathematics.

They're mature, stable, and work without fail.

They're also just about as fast as possible.

If you think you can do better, go ahead. You may very well come up with something that's better. But be sure when you're done developing your alternative, you compare the performance you get to the OS-supplied mutexes, semaphores and atomic operations, because coming up with something better is going to be very hard to do.

But if you can do it, it would be great.
# 5  
Old 12-28-2009
Quote:
Originally Posted by fpmurphy
Interlocked variables/operations are mostly a Microsoft win32 thing. Because some architectures capable of running Linux do not have the necessary hardware support, interlocked variables/operations are not available in Linux user space. They are available only in the kernel and only on some architectures.
If I'm reading it right, an interlocked variable looks a lot like a linux futex, the atomic part of which does happen in userspace.
# 6  
Old 12-29-2009
Quote:
If I'm reading it right, an interlocked variable looks a lot like a linux futex
You are right. But there are some interesting differences.

A futex (fast userspace mutex) is essentially a userspace 32-bit or 64-bit address (lock) located in a shared memory region together with a matching kernel wait queue (futex queue). In the noncontended case, a lock can be acquired/released from userspace without having to enter the kernel. In the contended case, the kernel gets involved.

Implementation-wise, futexes are closer to slim reader/writer (SRW) locks than to interlocked variables.
# 7  
Old 01-15-2010
Quote:
Originally Posted by rvan
To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment.
Yes. For instance on SMP machine, you can use so-called "lock free algorithms". There are no standard library for those algorithms yet, and they are highly HW dependent. So unless you have an absolute need for performance, I would recommend against it and stick to classical synchronization devices like mutex, spinlock etc.

Cheers,
Loïc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

pthread and mutex question

Hello, I have got some issue with the struct variable with passed arguments the variable in the sturct is only recognize the last value their assigned to I'm pretty confused why the mutex didn't work out here is my program: #include<stdio.h> #include<pthread.h> pthread_mutex_t lock... (3 Replies)
Discussion started by: michael23
3 Replies

2. Programming

Mutex lock question

Hi all, I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation. Below is the summary of my code : MUTEX LOCK performTask(); MUTEX UNLOCK. In some cases I get into the situation where... (2 Replies)
Discussion started by: cjjoy
2 Replies

3. Programming

Mutex will not work

I am trying to use mutex in my multi-tread project, but they don't seem to work. I have created a simple demonstration of the problem. This is NOT how I would use a mutex, only a demonstration of the problem: #include <stdio.h> #include <pthread.h> int main() { int val; ... (3 Replies)
Discussion started by: ChrisWilliams
3 Replies

4. Shell Programming and Scripting

Mutex in Perl

Hello Everyone, I just joined this forum and this is my first post. I would like to know how can I impliment basic read/write locks in perl. I have a database (file) which can be accessed simultaneously but has to be locked while writing. If there is no such support in perl, my next... (6 Replies)
Discussion started by: superuser84
6 Replies

5. UNIX for Dummies Questions & Answers

what is diff b/w semaphore and mutex

can u tell me what is the exact difference b/w mutex and semaphore and what is the diff b/w counting semaphore and binary semaphore amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

6. Programming

difference between Mutex and semaphores

Hi, can someone explain me the difference between mutex and semaphores? Thanks (1 Reply)
Discussion started by: rvan
1 Replies

7. Programming

How to handle mutex lock?

Hi, I have two tasks 'Read' and 'Write' which reads and writes on a file "abc.txt" respectively. Now I need to restrict the Write operation on the file while Read is going on, But can allow two Reads at a time. ie. two Reads can happen simultaneously, but Write can't happen at Read is going on. ... (3 Replies)
Discussion started by: satheeshalle
3 Replies

8. Shell Programming and Scripting

mutex in shell programing

A shell in crontab per 5 min write a file B shell in crontab per 6 min read a file how to lock the share file a ;avioid confilict in write and read? Thx : -) (1 Reply)
Discussion started by: zz_xm
1 Replies

9. UNIX for Dummies Questions & Answers

mutex

Can anyone explain me what mutexes are in multithreading environment? (2 Replies)
Discussion started by: sagar
2 Replies

10. Programming

Threads and Mutex

Hi all, I am working in a UNIX/C environment. I would like to understand more about MUTEX and Threads. Can someone explain me these concepts and how they are related. Vijay (2 Replies)
Discussion started by: vthasan
2 Replies
Login or Register to Ask a Question