rather than sizeof(std::atomic<bool>)==1
?
A mutex could be implemented via a simple std::atomic<bool>
, so I would think that the size of a mutex could be as small as that, or perhaps 4 (32bits).
rather than sizeof(std::atomic<bool>)==1
?
A mutex could be implemented via a simple std::atomic<bool>
, so I would think that the size of a mutex could be as small as that, or perhaps 4 (32bits).
With one bool
you could only implement a spin-lock. Note that it would be an unfair lock because nothing ensures that waiters queue up, so there is a chance that under high contention in the most extreme case a thread could be blocked forever because it would always lose the race to acquire the lock.
A mutex implementation needs support from the operating system to be able to put the waiting threads to sleep. So, a mutex would need a flag telling whether it is locked and some form of a queue descriptor that allows putting waiting threads to sleep and waking them. If you want the mutex to be able to support recursive locking, robustness, optional spinning, priority inversion protection, etc.., it would need even more members.
int
and a system call like futex
on Linux, you can implement a proper mutex. –
Hoffmann int
"? I'll have to disagree if you mean the first. –
Hoffmann std::mutex
itself has the same precondition, with UB if you break it. And it's very easy to enforce externally, using an RAII lock type. –
Hoffmann EPERM
. –
Plassey int
. But the question isn't about a Posix mutex, it's about std::mutex
which (like much of the C++ library) doesn't require preconditions to be enforced. –
Hoffmann The GNU library usually uses Posix threads to implement the standard thread library. That uses a single type pthread_mutex_t
to represent several different types of mutex; so it contains various fields needed for more complex mutexes (e.g. a counter for recursive mutexes), plus a field to specify the type.
You're right that, in principle, with suitable support from the operating system, a std::mutex
could use as little as one byte of user memory. (On Linux, it has to be an int
; and on other platforms, it might be an integer or pointer-sized handle to a kernel resource). Presumably, the benefits of using a well-tested existing implementation were deemed to outweigh the benefits of saving a few dozen bytes per mutex.
A mutex could be implemented via a simple
std::atomic<bool>
It does not look like a possibility, considering that mutex::lock
is a required operation, and std::atomic<bool>
is most likely a non-locking kind. You could put a while
loop around the compare_exchange_strong
call, but that is not the same as mutex::lock
, because it wastes the CPU during the entire waiting period.
In general, std::mutex
is a lot more complex than a simple bool
with defined multithreaded behavior, explaining its rather larger size, which depends on the compiler: for example, on ideone the sizeof(mutex)
is 24.
© 2022 - 2024 — McMap. All rights reserved.
std::atomic<bool>
" doesn't mean it's the optimal implementation. – Lunaticatomic<bool>
is a poor man'smutex
... I mean, really poor man's one. – Arnoldstd::mutex
isn't implemented as a spinlock. – Elizbethmutex
. Spinlock is a poor man'smutex
- that is, if you want a mutex, it is a poor choice, not "it is a poor choice in general". – Arnold