Questions tagged [stdmutex]
44 questions
505
votes
7 answers
std::unique_lock or std::lock_guard?
I have two use cases.
A. I want to synchronise access to a queue for two threads.
B. I want to synchronise access to a queue for two threads and use a condition variable because one of the threads will wait on content to be stored into the queue by…

chmike
- 20,922
- 21
- 83
- 106
15
votes
2 answers
Is there any reason C++ 11+ std::mutex should be declared as a global variable instead of passed into a std::thread as a function parameter?
I've seen most examples using std::mutex where the mutex is global. I was wondering is there any specific reason why this is done? I've had programs of my own where I don't do this, and simply pass the mutex in as a std::thread std::ref. Isn't it…

Krupip
- 4,404
- 2
- 32
- 54
14
votes
2 answers
How is std::atomic_ref implemented for non-atomic types?
I am wondering how can std::atomic_ref be implemented efficiently (one std::mutex per object) for non-atomic objects as the following property seems rather hard to enforce:
Atomic operations applied to an object through an atomic_ref are atomic…

Nonyme
- 1,220
- 1
- 11
- 22
13
votes
1 answer
Why is std::mutex so much worse than std::shared_mutex in Visual C++?
Ran the following in Visual Studio 2022 in release mode:
#include
#include
#include
#include
std::mutex mx;
std::shared_mutex smx;
constexpr int N = 100'000'000;
int main()
{
auto t1 =…

Alex Guteniev
- 12,039
- 2
- 34
- 79
13
votes
1 answer
Why is std::mutex a standard-layout class?
[thread.mutex.class]/3:
[...] It is a standard-layout class ([class.prop]).
What is the reason for this requirement?

Alex Guteniev
- 12,039
- 2
- 34
- 79
12
votes
1 answer
Is a copy-on-return operation executed prior or after lock_guard destructor?
Is the get_a() function safe for race-conditions or do I need to explicitly copy str_ as in get_b() in order to have a thread-safe function?
class Class {
public:
auto get_a() -> std::string {
auto&& guard = std::lock_guard{mutex_};
return…

Viktor Sehr
- 12,825
- 5
- 58
- 90
11
votes
1 answer
Using std::mutex as member variable in a class
I have defined a class that has std::mutex my_mutex as its private member variable. But when I try to use it using lock_guard in a member function which is called from different threads, the compiler throws up a lots of errors. If I keep this mutex…

Prashant
- 1,144
- 8
- 17
- 28
10
votes
1 answer
Why do functions using std::mutex make a null check of the address of pthread_key_create?
Take this simple function that increments an integer under a lock implemented by std::mutex:
#include
std::mutex m;
void inc(int& i) {
std::unique_lock lock(m);
i++;
}
I would expect this (after inlining) to compile in…

BeeOnRope
- 60,350
- 16
- 207
- 386
8
votes
4 answers
Is my wait - notify mechanism using std::mutex correct?
I started using std::mutexes to stop a thread and wait for another thread to resume it. It works like this:
Thread 1
// Ensures the mutex will be locked
while(myWaitMutex.try_lock());
// Locks it again to pause this…

Tomáš Zato
- 50,171
- 52
- 268
- 778
6
votes
2 answers
Why is sizeof std::mutex == 40 when cache line size is often 64 bytes
Following static_assert passes in both gcc and clang trunk.
#include
int main(){
static_assert(sizeof(std::mutex)==40);
}
Since x86 CPUs have 64 byte cache line I was expecting mutex sizeof to be 64, so false-sharing can be avoided.
Is…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
6
votes
1 answer
Different behavior when `std::lock_guard` object has no name
I'm learning about std::mutex, std::thread and I am surprised at the different behavior of 2 pieces of code below:
#include
#include
#include
using namespace std;
std::mutex mtx;
void foo(int k)
{
…

rsy56640
- 299
- 1
- 3
- 13
6
votes
3 answers
std::mutex in shared memory not working
I have a scenario where the shared memory area is exclusively accessed by two different processes. When I launch the processes, the first process successfully locks the mutex, updates the memory and unlock the mutex. But I observe that when the…

klekle
- 81
- 1
- 5
5
votes
1 answer
Is c++ singleton need memory barrier while using mutex?
I have known that mutex can also bring the effect as memory barrier from here: Can mutex replace memory barriers, but I always see there is an memory barrier using in c++ singleton example as below, is the memory barrier unnecessary?
Singleton*…

woder
- 647
- 5
- 12
5
votes
1 answer
Why did CRITICAL_SECTION performance become worse on Win8
It seems like CRITICAL_SECTION performance became worse on Windows 8 and higher. (see graphs below)
The test is pretty simple: some concurrent threads do 3 million locks each to access a variable exclusively. You can find the C++ program at the…

Rom098
- 2,445
- 4
- 35
- 52
5
votes
4 answers
Why the constructor of std::mutex in C++ does not throw?
The pthread_mutex_init() function returns a non-zero value when it fails to initialize the mutex, while the std::mutex class in C++11 has a constructor of noexcept.
Say one chooses to implement a C++ mutex class on top of pthreads mutex. He wraps…

John Z. Li
- 1,893
- 2
- 12
- 19