我使用以下代码在运行于 debian 8 上的 c++ 应用程序中创建了一个计时器对象。
class Timer
{
private:
std::condition_variable cond_;
std::mutex mutex_;
int duration;
void *params;
public:
Timer::Timer(void (*func)(void*))
{
this->handler = func;
this->duration = 0;
this->params = NULL;
};
Timer::~Timer(){};
void Timer::start(int duree, void* handlerParams)
{
this->duration = duree;
this->params = handlerParams;
/*
* Launch the timer thread and wait it
*/
std::thread([this]{
std::unique_lock<std::mutex> mlock(mutex_);
std::cv_status ret = cond_.wait_for(mlock,
std::chrono::seconds(duration));
if ( ret == std::cv_status::timeout )
{
handler(params);
}
}).detach();
};
void Timer::stop()
{
cond_.notify_all();
}
};
它在 gdb 和正常条件下正常工作,但在 30 个或更多请求的负载测试中,它因断言而崩溃:
nptl/pthread_mutex_lock.c:350: __pthread_mutex_cond_lock_full: 断言`(-(e)) != 3 || !robust' 失败。
我不明白这个断言的原因。谁能帮帮我?? 谢谢
最佳答案
基本上你有一个访问定时器对象的分离线程,所以你很可能销毁了定时器对象但线程仍在运行并访问它的成员(互斥锁,条件变量)。
根据 glibc 源代码,断言本身表明互斥体的所有者已经死亡。
关于c++ - 在负载测试中对 __pthread_mutex_cond_lock_full 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40739157/