Thread View: comp.lang.c
4 messages
4 total messages
Started by Bonita Montero
Wed, 02 Dec 2026 11:58
Why ordered mutexed don't work ...
Author: Bonita Montero
Date: Wed, 02 Dec 2026 11:58
Date: Wed, 02 Dec 2026 11:58
11 lines
1934 bytes
1934 bytes
Just for fun, I tried writing a mutex today that wakes up threads in the same order they began waiting. I thought I was finished, but while cycling into the old town for a meal, it occurred to me: I can control the order in which threads start waiting, but I cannot control the order in which the scheduler wakes them up. Furthermore, if there isn’t a free CPU core available, there’s no guarantee that a thread will be woken up immediately. Once I got home, well-fed, I deleted the entire project. In any case, no operating system offers such mutexes, and runtime environments don’t provide them either. I suspect I’m not the first person to come up with this idea.
Re: Why ordered mutexed don't work ...
Author: David Brown
Date: Wed, 02 Dec 2026 12:47
Date: Wed, 02 Dec 2026 12:47
24 lines
2699 bytes
2699 bytes
On 02/12/2026 12:58, Bonita Montero wrote: > Just for fun, I tried writing a mutex today that wakes up threads > in the same order they began waiting. I thought I was finished, but > while cycling into the old town for a meal, it occurred to me: I can > control the order in which threads start waiting, but I cannot control > the order in which the scheduler wakes them up. Furthermore, if there > isn’t a free CPU core available, there’s no guarantee that a thread > will be woken up immediately. Once I got home, well-fed, I deleted > the entire project. > In any case, no operating system offers such mutexes, and runtime > environments don’t provide them either. I suspect I’m not the first > person to come up with this idea. I find it impressive that you know such details about the many hundreds or thousands of operating systems that exist, including all proprietary ones that are only known within a single company. Or by "no OS", did you mean Windows doesn't document such behaviour, and that's all the checking you need to claim it is universal? Resuming the blocked threads in FIFO order of blocking is the norm in RTOS's, and is typically guaranteed by the OS. Which resumed thread is scheduled to run then depends on the priorities of all runable threads (including the one that just release the mutex, as it is still runable).
Re: Why ordered mutexed don't work ...
Author: Bonita Montero
Date: Wed, 02 Dec 2026 13:09
Date: Wed, 02 Dec 2026 13:09
8 lines
1781 bytes
1781 bytes
Am 02.12.2026 um 13:47 schrieb David Brown: > Resuming the blocked threads in FIFO order of blocking is > the norm in RTOS's, and is typically guaranteed by the OS. ... Show me a RTOS with a guarantee on the ordering of wakeups with mutexes. It does't make sense since the behaviour of threads sharing the same mutex largely depends on their locking- and unlocking-timing.
Re: Why ordered mutexed don't work ...
Author: David Brown
Date: Wed, 02 Dec 2026 13:49
Date: Wed, 02 Dec 2026 13:49
50 lines
3818 bytes
3818 bytes
On 02/12/2026 14:09, Bonita Montero wrote: > Am 02.12.2026 um 13:47 schrieb David Brown: > >> Resuming the blocked threads in FIFO order of blocking is >> the norm in RTOS's, and is typically guaranteed by the OS. ... > > Show me a RTOS with a guarantee on the ordering of wakeups with mutexes. > It does't make sense since the behaviour of threads sharing the same > mutex largely depends on their locking- and unlocking-timing. From the ThreadX documentation on mutexes : """ After the same number of put operations are performed by the owning thread, the suspended thread’s get operation is performed, giving it ownership of the mutex, and the thread is resumed. If multiple threads are suspended on the same mutex, they are resumed in the same order they were suspended (FIFO). """ I only picked ThreadX, because I happen to be using it in a project at the moment, so I have the documentation open. Some other RTOS's will resume threads based on their priority, but if multiple threads of the same priority are waiting for the same mutex, they will be resumed in FIFO order. If thread 1 takes a mutex, and then threads 2, 3 and 4 all try to take it and block, then thread 1 releases the mutex, there are only two sensible real-time strategies for picking which blocked thread gets released. You either picked the thread that has been waiting longest (FIFO first), or you pick the blocked thread with the highest priority, and if there are multiple blocked threads with the same priority, pick the one that has been waiting longest. ThreadX uses the first of these (FIFO, always waking the thread that has waited longest), while FreeRTOS uses the later (use priority first, then FIFO if there are multiple waiting threads with the same priority). AFAIUI, real-time threads on Linux do this too, when using real-time mutexes. Most OS's are RTOS's, and that means such matters are documented and guaranteed. For common lock types on "big" systems, there is too much variation to be able to guarantee exact ordering without extra overheads. Such OS's may have additional effects they take into account - thread affinity to different cpus and caches, overall waiting times for a thread, expected run-times for the thread, etc. But if there are no overriding factors, I'd expect to see roughly - but not guaranteed - FIFO ordering.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads