FAQ Database Discussion Community

Condition variable's notification is missed

At the moment I am writing some kind of Fork/Join pattern using std::threads. Therefore I wrote a wrapper class for std::thread which uses a reference counter for all children. Whenever a child finishes its execution the reference counter is decremented and a notification is sent to all waiting threads. The...

std::condition_variable::wait_until function

I have a small question about using this method. Will my thread wake up if the std::chrono::high_resolution_clock::now() is greater then the second parameter(abs_time)?

C++ condition variable wait_for not behaving as expected

I am having a problem understanding why a test case that I think should be passing is failing most of the time. I have distilled the test down to the condition variable and using the wait_for method, specifically testing it to see if it does indeed wait for at least...

Code won't advance past wait function for conditional variable

So, I've been trying to get a better understand of how condition variables work and I've written the following code that tries to implement reading and writing from the same text file: #include <iostream> #include <fstream> #include <cstdlib> #include <condition_variable> #include <mutex> #include <thread> using namespace std; mutex mtx; condition_variable...

notify_all_at_thread_exit doesn't exist in Cygwin GCC

When I try to build the following simple test program in 32-bit Cygwin with GCC version 4.9.2, I get an error saying: error: ‘notify_all_at_thread_exit’ is not a member of ‘std’ This makes me think that this specific method hasn't been ported to Cygwin. Does anyone know if I am either...

VC++ 11 - std::condition_variable_any not compatible with standard?

Recently, I ran into strange compilation issues on Microsoft Visual Studio 2012 (equipped with Visual C++ 11). I ported large code base, developed mostly on Linux with extensive C++ 11 usage in mind. Despite few minor changes, everything was working fine. Except one thing: I got strange error about result...

What if a condition variable signals to a locked thread?

In the (pseudo-)code below, cond might wake up while it shouldn't, for whatever reason. So I put a while loop there. When it does wake up, it will still consume the lock, so it is guaranteed that in out() only one thread is doing its job. But what happens if,...

Reader doesn't wake up once writer finishes writting in Reader writer lock

class ReadLock { private: std::mutex readWriteMutex; std::mutex conditionmtx; std::condition_variable cv; int readings = 0; int writings = 0; int writers = 0; public: void AquireReadLock() { readWriteMutex.lock(); if (writers) { std::unique_lock<std::mutex> lck(conditionmtx); cv.wait(lck); } while (writings) { std::unique_lock<std::mutex> lck(conditionmtx); cv.wait(lck); } readings++; readWriteMutex.unlock(); } void ReleaseReadLock() { readWriteMutex.lock(); //std::unique_lock<std::mutex> lck(conditionmtx);...

After signaling a waiting condition variable, when the thread will acquire the lock? What determines it?

After searching on the web, and reading the question answers on the stackoverflow, i couldn't find the answer of my question. The wait is called in thread B, and it unlocks the mutex, allowing others access to the condition variable (for signalling). Then, when the condition variable is signalled in...

Unexpected behavior while using condition variable multithread

In the code below: #include <stdio.h> #include <pthread.h> pthread_mutex_t mtx; pthread_cond_t cond; int how_many = 10; int pool = 0; void * producer(void * ptr) { while (how_many > 0) { pthread_mutex_lock(&mtx); printf("producer: %d\n", how_many); pool = how_many; how_many--; pthread_mutex_unlock(&mtx); pthread_cond_signal(&cond); } pthread_exit(0); } void * consumer(void * ptr) {...