c++ - 在负载测试中对 __pthread_mutex_cond_lock_full 断言失败

标签 c++ linux pthreads debian mutex

我使用以下代码在运行于 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/

相关文章:

c - 仅在 pthread 初始化时发生段错误

c++ - 我如何在 linux (debian) 下为 ipod/iphone 编译 c++ 应用程序

linux - Haskell System.Process 处理

linux - 在 Wine 中构建 MSI

c# - Rider IDE 找不到 .NET Core 安装

c - 如何从多线程并行的函数中获取一个正确的值

pthreads - pthread_create 和 EAGAIN

c++ - vector.push() 和 vector.reserve() 上的 bad_alloc

c++ - 无法将引用从一个类传递到另一个类

c++ - 什么时候对标量调用 stable_sort()?