Questions tagged [stdmutex]

29 questions
389
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
  • 17,648
  • 19
  • 66
  • 93
13
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
  • 3,570
  • 1
  • 27
  • 40
11
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,172
  • 3
  • 52
  • 78
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
  • 51,419
  • 13
  • 149
  • 309
9
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,118
  • 9
  • 20
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
  • 269
  • 1
  • 3
  • 11
5
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
  • 23,776
  • 22
  • 102
  • 222
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,187
  • 3
  • 28
  • 46
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,515
  • 1
  • 9
  • 17
5
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…
4
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
  • 61
  • 1
  • 4
4
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
  • 744
  • 4
  • 12
  • 21
3
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
  • 311
  • 2
  • 7
3
votes
2 answers

Why does a redundant extra scoping block affect std::lock_guard behaviour?

This code demonstrates that the mutex is being shared between two threads, but something weird is going on with the scoping block around thread_mutex. (I have a variation of this code in another question, but this seems like a second…
spraff
  • 29,265
  • 19
  • 105
  • 197
3
votes
2 answers

Why is std::mutex taking a long, highly irregular amount of time to be shared?

This code demonstrates that the mutex is being shared between two threads, but one thread has it nearly all of the time. #include #include #include #include int main () { std::mutex m; std::thread t…
spraff
  • 29,265
  • 19
  • 105
  • 197
1
2