Questions tagged [stdmutex]

44 questions
4
votes
1 answer

Release and Acquire with std::mutex

This is a question about the C++ standard. I only have access to the draft standard so if this is different in the official one, I apologise. Also if I've misunderstood how this works please feel free to correct me. Assume I have two threads, one…
jcoder
  • 29,554
  • 19
  • 87
  • 130
3
votes
4 answers

Why can mutex be used in different threads?

Using (writing) same variable in multiple threads simultaneously causes undefined behavior and crashes. Why using mutex, despite on fact that they are also variables, not causes undefined behavior? If mutex somehow can be used simultaneously, why…
Ngdgvcb
  • 155
  • 6
3
votes
2 answers

Relative performance of std::atomic and std::mutex

I am considering options for implementation of a queue for a project, a requirement of which is that the producer, at least, must be as low latency as possibe. To this end, I have been investigating "lock free" queues using std::atomic to control…
Pete
  • 62
  • 4
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
  • 32,570
  • 22
  • 121
  • 229
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
  • 32,570
  • 22
  • 121
  • 229
2
votes
3 answers

Is there any way I can lock a list of mutex?

My code is like the following. A Element holds a pointer to a mutex, and there is a vector that holds all Elements. If I know the count of elements at compile time, I can construct a scoped_lock to lock all these mutexes. However, I don't know the…
calvin
  • 2,125
  • 2
  • 21
  • 38
2
votes
2 answers

Why does libc++ allow recursive locking of std::mutex?

std::mutex is nonrecursive, and violation of that is UB. So anything is possible in theory(including works as std::recursive_mutex)), but libc++ seems to work fine , this program outputs bye #include #include std::mutex m; int…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
2
votes
1 answer

C++ Mutex locking for a set of threads

I understand the basic principle of mutex's is to protect specific code path from being accessed by more than 1 threads. In my current setup, I have 10 threads of type A (A1-A10), and 10 threads of type B(B1-B10). By type I mean, threads that are…
sublime
  • 4,013
  • 9
  • 53
  • 92
2
votes
3 answers

C++ sync between two threads using a mutex without using conditional variable

I have one thread that needs to be blocked until something happens in another thread. It sounds very typical and I have this solution. //thread 1 mux.lock(); //send work to another thread mux.lock(); //will likely block which I want //thread…
Sclaker
  • 69
  • 4
2
votes
2 answers

why use std::atomic if it still requires a mutex to work properly

Reading the text for std::condition_variable I've come across this sentence: Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread. My question is this: What…
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
1
vote
2 answers

I cannot use mutex along with vector

I have trouble compiling the following code. #include #include class A { public: A(){} private: std::mutex a_mutex; }; int main() { std::vector v; A a; v.push_back(a); return 0; } This is the error. $ g++…
user180574
  • 5,681
  • 13
  • 53
  • 94
1
vote
1 answer

Questions regarding lock-free vs mutex solution for Leetcode 1117: Building H2O

I'm working on practicing C++ concurrency and learning about the practical applications of using mutexes vs lock free atomics. I was working on the following problem from Leetcode: https://leetcode.com/problems/building-h2o/description/ Basically,…
bot654321
  • 39
  • 4
1
vote
0 answers

Is there a need to provide a std::mutex for std::async?

As stuff I mentioned above, Should I prodive a std::mutex for std::async in the following section? : #include #include #include #include int main() { [[maybe_unused]] result=::std::async([&] { …
Savner_Dig
  • 21
  • 1
  • 8
1
vote
1 answer

Why does introducing std::mutex to member class generate this compile error?

In the code below, class B contains an array of member class class A. B::A has one member bool and one member std::thread. The code below compiles fine: // main.cpp #include #include class B { public: B(); private: class A { …
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
1
vote
1 answer

HOW to solve "'mutex' in namespace 'std' does not name a type"?

My C++ file is as following: class LogMessage { public: LogMessage(const char* file, int line) : #ifdef __ANDROID__ log_stream_(std::cout) #else log_stream_(std::cerr) #endif { std::unique_lock lk(mu_); …
Alfred
  • 71
  • 5