🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2104

Re: Why ordered mutexed don't work ...

#2104
From: David Brown
Date: Wed, 02 Dec 2026 19:57
34 lines
2973 bytes
On 02/12/2026 18:06, Kaz Kylheku wrote:
> On 2026-12-02, Bonita Montero <Bonita.Montero@gmail.com> 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.
> 
> The problem is racing between the waiting thread which your mutex
> algorithm chose to wake up (e.g. FIFO) and one or more non-waiting
> threads that just happened to come along trying to get the mutex.
> 

I can see that being a potential problem with some kinds of mutexes in 
general-purpose OS's, but it is not an issue for RTOS mutexes (including 
Linux's rt mutexes).  With those, it is the OS that picks which waiting 
thread should be resumed when the owning thread releases the mutex, and 
it gives ownership to the specific thread before resuming it.  The 
release from one thread and acquire in the next thread are effectively 
atomic - no other thread can grab the mutex in the meantime.

For mutexes that are implemented as a hybrid of a user-mode spinlock and 
OS mutex, there might be a brief window in which another thread can grab 
the mutex between the old thread releasing it and the waiting thread 
locking it.  But not for RTOS's.

> If you want strict order, you have to eliminate the possibility of
> creating a time window where a running thread can opportunistically grab
> a mutex that you're trying to pass to the next waiting thread.
> 

Yes.

Technical Details
Message-ID: <11ept6u$3eg78$2@dont-email.me>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11ep150$2vi4k$1@raubtier-asyl.eternal-september.org> <20261202073550.188@kylheku.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Why ordered mutexed don't work ...
Date: Wed, 2 Dec 2026 20:57:18 +0100
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <11ept6u$3eg78$2@dont-email.me>
References: <11ep150$2vi4k$1@raubtier-asyl.eternal-september.org>
 <20261202073550.188@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 02 Dec 2026 19:57:18 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3621096"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19nDPCGmwlZWkNi/wqqZ2DV2vwmsbJ68d4="; posting-host="8f689f606fabca0b227a4c433cd7c78e"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:vesLCbDr0GG8XbkW34bEjCzoQcg= sha256:V10nwi7zthguiVnFaQ1KrIFigraVo0uZEkIfiTR/rsQ=
	sha1:DKyVdgIrdEVxyhGF2cWH6QITA/w= sha256:v68cpFO4g3NUlk1/2+npVhP1FdIyWWczeeStIx5b91Y=
In-Reply-To: <20261202073550.188@kylheku.com>
Content-Language: en-GB