Article View: comp.lang.c++
Article #1105Re: Fwd: futexes vs. system semaphores
From: Chris M. Thomasson
Date: Thu, 03 Dec 2026 21:40
Date: Thu, 03 Dec 2026 21:40
69 lines
3392 bytes
3392 bytes
On 12/3/2026 1:39 PM, Chris M. Thomasson wrote:
> On 12/2/2026 11:09 PM, Bonita Montero wrote:
>> I extended my benchmark to test futexes not only with two threads
>> but with two threads up to hardware_concurrency() threads. Under
>> Linux the performance with three threads up to hc is exactly the
>> same as with a Pthread semaphore. Under Windows the performance
>> drops to 50% the performance of futexes with three threads and
>> above.
>>
>
> You should measure how many times the fast path gets hit:
>
> void FtxSema::acquire()
> {
> for( int ftx = m_ftx; ; )
> if( ftx )
> if( m_ftx.compare_exchange_strong( ftx, 0 ) )
> return;
> else
> continue;
> else
> {
> m_ftx.wait( 0 );
> ftx = m_ftx;
> }
> }
>
>
> Also, you need to try to fast path the release:
>
> void FtxSema::release()
> {
> m_ftx = 1;
> m_ftx.notify_one();
> }
>
>
> Your futex sema is not all that good.
>
> Also, add this to your test bed:
>
> https://vorbrodt.blog/2019/02/05/fast-semaphore/
class fast_semaphore
{
public:
fast_semaphore(int count) noexcept
: m_count(count), m_semaphore(0) {}
void post()
{
std::atomic_thread_fence(std::memory_order_release);
int count = m_count.fetch_add(1, std::memory_order_relaxed);
if (count < 0)
m_semaphore.post();
}
void wait()
{
int count = m_count.fetch_sub(1, std::memory_order_relaxed);
if (count < 1)
m_semaphore.wait();
std::atomic_thread_fence(std::memory_order_acquire);
}
private:
std::atomic m_count;
semaphore m_semaphore;
};
Technical Details
Message-ID:
<11esnkj$s9c7$3@dont-email.me>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11duih8$sm6c$1@raubtier-asyl.eternal-september.org> <11duq79$vfpt$1@raubtier-asyl.eternal-september.org> <11er4jp$10h0$1@raubtier-asyl.eternal-september.org> <11esnho$s9c7$2@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c++ Subject: Re: Fwd: futexes vs. system semaphores Date: Thu, 3 Dec 2026 13:40:34 -0800 Organization: A noiseless patient Spider Lines: 69 Message-ID: <11esnkj$s9c7$3@dont-email.me> References: <11duih8$sm6c$1@raubtier-asyl.eternal-september.org> <11duq79$vfpt$1@raubtier-asyl.eternal-september.org> <11er4jp$10h0$1@raubtier-asyl.eternal-september.org> <11esnho$s9c7$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 03 Dec 2026 21:40:36 +0000 (UTC) Injection-Info: dont-email.me; logging-data="927111"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Lt2hAKG1xjPqziw4AZ5+3L1yxhqbA9o0="; posting-host="27be630b90107ef0dd0e5ccb8f536ba4" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:8VxTte20dc3bVq8RJ8afCWZhSaw= sha256:iTWsO+GWfUhTgyxew8FmwJDR8xbh2lcUy8G1uFj0brI= sha1:azCR4PcXPvgZ1aA/s7m1+dDdsDE= sha256:TvkgEVcH+LY/EXYaxotpyItXgRyCpc1N1c3sBH9wikY= Content-Language: en-US In-Reply-To: <11esnho$s9c7$2@dont-email.me>