我有一个非常简单的C ++程序,如下所示。 A,B和C类位于DLL中。当我关闭这个应用程序时,它有时会在条件变量上调用notify_all()时崩溃。任何人都可以给我一个暗示的原因吗?
我在SO上检查了很多Q / As,但没有一个能解决我的问题。我在Windows 7和VS2013上工作。
class B;
class C
{
public:
C(const std::weak_ptr<B>& b) : mB(b)
{
}
virtual ~C()
{
}
void run()
{
while (true)
{
std::unique_lock<std::mutex> uLock(mMutex);
// Wait until some event is happening
mCondition.wait_for(uLock, std::chrono::seconds(300));
if (!mStop)
{
//do something here
}
else
{
return;
}
}
}
void start()
{
mThread = std::thread(&C::run, this);
}
void stop()
{
mStop = false;
}
void notify()
{
mCondition.notify_all();
}
void join()
{
if (mThread.joinable())
{
mThread.join();
}
}
private:
std::atomic<bool> mStop;
std::condition_variable mCondition;
std::mutex mMutex;
std::thread mThread;
std::weak_ptr<B> mB;
};
class B : public std::enable_shared_from_this<B>
{
public:
B() {}
~B()
{
if (mC)
{
mC->stop();
mC->notify();
mC->join();
}
}
// basic methods
void init()
{
mC = std::unique_ptr<C>(new C(shared_from_this()));
mC->start();
}
private:
std::unique_ptr<C> mC;
};
class A
{
public:
~A(){}
void init() { pImpl->init(); }
static std::shared_ptr<A> getInstance(){
static std::shared_ptr<A> instance(new A);
return instance;
}
private:
A() : pImpl(std::make_shared<B>()){}
std::shared_ptr<B> pImpl;
};
void main()
{
std::shared_ptr<A> a = A::getInstance();
a->init();
int x;
std::cin >> x;
}
编辑1:如果我将B的析构函数中的代码放在另一个函数中(例如clean())并从main()调用它(使用A中的clean()方法)不会发生崩溃。
代码错过了条件变量通知,因为:
stop_ = true
期间不会持有互斥锁(它应该是true
,而不是false
)。必须在保持互斥锁的同时读取和修改stop_
,并且它不需要是原子的。当人们使用原子与互斥和条件变量时,这是竞争条件的常见原因。修正:
class B;
class C
{
public:
C(const std::weak_ptr<B>& b) : mB(b) {}
~C() { stop(); }
void run()
{
while (true) {
std::unique_lock<std::mutex> uLock(mMutex);
while(!mStop /* && !other_real_condition */)
mCondition.wait_for(uLock, std::chrono::seconds(300));
if(mStop)
return;
// other_real_condition is true, process it.
}
}
void start()
{
mThread = std::thread(&C::run, this);
}
void stop()
{
{
std::unique_lock<std::mutex> uLock(mMutex);
mStop = true;
}
mCondition.notify_all();
if (mThread.joinable())
mThread.join();
}
private:
bool mStop = false; // <--- do not forget to initialize
std::condition_variable mCondition;
std::mutex mMutex;
std::thread mThread;
std::weak_ptr<B> mB;
};
class B : public std::enable_shared_from_this<B>
{
public:
// basic methods
void init()
{
mC = std::unique_ptr<C>(new C(shared_from_this()));
mC->start();
}
private:
std::unique_ptr<C> mC;
};
如果在不保持互斥锁的情况下设置mStop
,则会发生以下情况:
| Thread 1 | Thread 2 |
| mStop = true | |
| mCondition.notify_all | |
| | mMutex.lock |
| | mCondition.wait_for |
在上面的线程2丢失通知并等待虽然设置了mStop
。
更新共享状态时锁定互斥锁可修复该竞争条件:
| Thread 1 | Thread 2 |
| mMutex.lock | |
| mStop = true | |
| mCondition.notify_all | |
| mMutex.unlock | |
| | mMutex.lock |
| | mStop == true, no wait |
在等待条件变量时,必须在保持互斥锁时修改和读取共享状态,否则条件通知会丢失并且可能导致死锁(等待时没有超时)。这就是为什么不必使用原子与互斥和条件变量,你要么使用原子或互斥和条件变量,而不是两者。
这似乎是一个CRT错误(https://stackoverflow.com/a/50525968/896012)。问题不会发生在新版本的Windows上,即Windows 10.为了修复Windows 7上的崩溃,我只是删除了condition_variable并使用了一个简单的睡眠而退出程序时我只是分离了线程。虽然这不是一个干净的方法,但我认为这是避免崩溃的唯一方法。如果有人找到了更好的答案,请告诉我。