Linked by Elad Lahav on Thu 18th Feb 2016 19:27 UTC
QNX

A mutex is a common type of lock used to serialize concurrent access by multiple threads to shared resources. While support for POSIX mutexes in the QNX Neutrino Realtime OS dates back to the early days of the system, this area of the code has seen considerable changes in the last couple of years.

Thread beginning with comment 624993
To read all comments associated with this story, please click here.
Mutex behavior
by Alfman on Fri 19th Feb 2016 00:10 UTC
Alfman
Member since:
2011-01-28

In a recent project, I encountered bugs due to undefined conditions of the pthread mutex.

I needed to capture a mutex, hold it across thread invocation, and then release it. Turns out that there's no defined behavior in this scenario.

http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_mutex_lo...

If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behaviour results.


So I opted to use the posix semaphore mechanisms sem_post sem_wait instead, which behaves as expected even under edge conditions.

I wish I could change one thing about posix semaphores: that one could wait on any number of events instead of decremented them one at a time. If 20 threads call sem_post(semaphore), the blocking thread has to call sem_wait 20 times just to decrement the counter back to zero.

while(true) {
sem_wait(semaphore);
// process thread events 20 times even if only the first iteration is productive
}


Turns out Linux kernel devs recognized this deficiency and corrected it with eventfd, although it's proprietary to linux and uses a new read/write syscall API.

Man, software development entails so many little factoids, if I had to start over, I don't even want to think about it ;)

Reply Score: 2

RE: Mutex behavior
by elahav on Fri 19th Feb 2016 01:27 in reply to "Mutex behavior"
elahav Member since:
2009-05-28

If you are waiting on an event that can be generated by any, or even multiple, threads, then semaphores are not what you are looking for. Perhaps a condition variable?

Also, note that semaphores and mutexes, while sometimes confused for one another, achieve completely different tasks: the first are a synchronization (i.e., temporal) mechanism, the second a data-protection (i.e., spacial) mechanism. Search for "Concurrent Urban Legends" by Peter Buhr and Ashif Harji.

Interestingly, your original goal, that of explicitly handing over a mutex from one thread to another without releasing it is something that has come up several times in the past. It's not hard to implement, I'm just not sure that the semantics of such an operation are fully understood.

Reply Parent Score: 1

RE[2]: Mutex behavior
by Alfman on Fri 19th Feb 2016 02:52 in reply to "RE: Mutex behavior"
Alfman Member since:
2011-01-28

elahav,

If you are waiting on an event that can be generated by any, or even multiple, threads, then semaphores are not what you are looking for. Perhaps a condition variable?


No actually, the purpose of the mutex was really just to protect a data structure from concurrent access.

Interestingly, your original goal, that of explicitly handing over a mutex from one thread to another without releasing it is something that has come up several times in the past. It's not hard to implement, I'm just not sure that the semantics of such an operation are fully understood.



Once the error was tracked to the mutex, it wasn't hard to fix: just replace the mutex with a posix semaphore.

sem_init(&sem, 1);
...
sem_wait(sem); // lock
// equivalent to mutex
sem_post(sem); // unlock (even in new thread)


It's semantically identical to the mutex, with the benefit of having defined behavior across threads.

Edited 2016-02-19 02:54 UTC

Reply Parent Score: 2