WiSaGaN WiSaGaN - 1 year ago 67
C++ Question

What happens when calling the destructor of a thread object that has a condition variable waiting?

I am using a

to communicate between threads. I found that destroying the thread object when the attaching thread is waiting on a condition variable would cause the program crash. This can be corrected by calling
before the thread destruction. But I am wondering what happens exactly when a thread waiting a conditional variable got terminated. Is there another way to use condition variable to avoid this?

#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>

template <typename Type> class SynchronisedQueue {
void Enqueue(Type const & data) {
std::unique_lock<std::mutex> lock(mutex_);
Type Dequeue() {
std::unique_lock<std::mutex> lock(mutex_);
while (queue_.empty())
Type result = queue_.front();
return result;
std::queue<Type> queue_;
std::mutex mutex_;
std::condition_variable condition_;

class Worker {
Worker(SynchronisedQueue<int> * queue) : queue_(queue) {}
void operator()() {
queue_->Dequeue(); // <-- The thread waits here.
SynchronisedQueue<int> * queue_;

int main() {
auto queue = new SynchronisedQueue<int>();
Worker worker(queue);
std::thread worker_thread(worker);
worker_thread.~thread(); // <-- Crashes the program.
return 0;

Answer Source

From the C++11 spec: thread destructor [thread.thread.destr] ~thread();

If joinable(), calls std::terminate(). Otherwise, has no effects.

[ Note: Either implicitly detaching or joining a joinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the pro grammer must ensure that the destructor is never executed while the thread is still joinable. — end note ]

So calling a thread destructor without first calling join (to wait for it to finish) or detach is guarenteed to immediately call std::terminate and end the program.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download