多线程 – 使用多线程和互斥锁时在互斥锁上断言

前端之家收集整理的这篇文章主要介绍了多线程 – 使用多线程和互斥锁时在互斥锁上断言前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
作为项目的一部分,我正在编写记录器功能.当程序想要记录某些内容时,此记录器功能会发送电子邮件.由于SMTP服务器没有响应,我决定在单独的线程中发送邮件.
该线程从std :: deque中读取消息,该消息由日志记录函数填充.
线程设置如下:
  1. while (!boost::this_thread::interruption_requested())
  2. {
  3. EmailItem emailItem;
  4. {
  5. boost::unique_lock<boost::mutex> lock(mMutex);
  6. while (mEmailBuffer.empty())
  7. mCond.wait(lock);
  8.  
  9. bufferOverflow = mBufferOverflow;
  10. mBufferOverflow = false;
  11. nrOfItems = mEmailBuffer.size();
  12.  
  13. if (nrOfItems > 0)
  14. {
  15. emailItem = mEmailBuffer.front();
  16. mEmailBuffer.pop_front();
  17. }
  18. }
  19.  
  20. if (nrOfItems > 0)
  21. {
  22. bool sent = false;
  23. while(!sent)
  24. {
  25. try
  26. {
  27. ..... Do something with the message .....
  28. {
  29. boost::this_thread::disable_interruption di;
  30. boost::lock_guard<boost::mutex> lock(mLoggerMutex);
  31. mLogFile << emailItem.mMessage << std::endl;
  32. }
  33. sent = true;
  34. }
  35. catch (const std::exception &e)
  36. {
  37. // Unable to send mail,an exception occurred. Retry sending it after some time
  38. sent = false;
  39. boost::this_thread::sleep(boost::posix_time::seconds(LOG_WAITBEFORE_RETRY));
  40. }
  41. }
  42. }
  43. }

函数log()向deque(mEmailBuffer)添加一条新消息,如下所示:

  1. {
  2. boost::lock_guard<boost::mutex> lock(mMutex);
  3. mEmailBuffer.push_back(e);
  4. mCond.notify_one();
  5. }

当主程序退出时,将调用记录器对象的析构函数.这是它出错的地方,应用程序崩溃并出现错误

  1. /usr/include/boost/thread/pthread/mutex.hpp:45: boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&m)' Failed.

析构函数只是在线程上调用一个中断然后加入它:

  1. mQueueThread.interrupt();
  2. mQueueThread.join();

在主程序中,我使用了多个不同的类,它们也使用了boost线程和互斥,这会导致这种行为吗?不调用logger对象的析构函数不会导致错误,就像使用logger对象而不执行任何其他操作一样.

我的猜测是我做了一些非常错误的事情,或者当使用多个线程划分多个线程时,线程库中存在一个错误.
有谁知道这个错误的原因是什么?

编辑:
我做了@Andy T提议并尽可能地删除代码.我删除了在不同线程中运行的函数中的几乎所有内容.线程现在看起来像:

  1. void Vi::Logger::ThreadedQueue()
  2. {
  3. bool bufferOverflow = false;
  4. time_t last_overflow = 0;
  5. unsigned int nrOfItems = 0;
  6.  
  7. while (!boost::this_thread::interruption_requested())
  8. {
  9. EmailItem emailItem;
  10. // Check for new log entries
  11. {
  12. boost::unique_lock<boost::mutex> lock(mMutex);
  13. while (mEmailBuffer.empty())
  14. mCond.wait(lock);
  15. }
  16. }
  17. }

问题仍然存在.然而,回溯问题显示我与初始代码有所不同:

  1. #0 0x00007ffff53e9ba5 in raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
  2. #1 0x00007ffff53ed6b0 in abort () at abort.c:92
  3. #2 0x00007ffff53e2a71 in __assert_fail (assertion=0x7ffff7bb6407 "!pthread_mutex_lock(&m)",file=<value optimized out>,line=50,function=0x7ffff7bb7130 "void boost::mutex::lock()") at assert.c:81
  4. #3 0x00007ffff7b930f3 in boost::mutex::lock (this=0x7fffe2c1b0b8) at /usr/include/boost/thread/pthread/mutex.hpp:50
  5. #4 0x00007ffff7b9596c in boost::unique_lock<boost::mutex>::lock (this=0x7fffe48b3b40) at /usr/include/boost/thread/locks.hpp:349
  6. #5 0x00007ffff7b958db in boost::unique_lock<boost::mutex>::unique_lock (this=0x7fffe48b3b40,m_=...) at /usr/include/boost/thread/locks.hpp:227
  7. #6 0x00007ffff6ac2bb7 in Vi::Logger::ThreadedQueue (this=0x7fffe2c1ade0) at /data/repos_ViNotion/stdcomp/Logging/trunk/src/Logger.cpp:198
  8. #7 0x00007ffff6acf2b2 in boost::_mfi::mf0<void,Vi::Logger>::operator() (this=0x7fffe2c1d890,p=0x7fffe2c1ade0) at /usr/include/boost/bind/mem_fn_template.hpp:49
  9. #8 0x00007ffff6acf222 in boost::_bi::list1<boost::_bi::value<Vi::Logger*> >::operator()<boost::_mfi::mf0<void,Vi::Logger>,boost::_bi::list0> (this=0x7fffe2c1d8a0,f=...,a=...) at /usr/include/boost/bind/bind.hpp:253
  10. #9 0x00007ffff6acf1bd in boost::_bi::bind_t<void,boost::_mfi::mf0<void,boost::_bi::list1<boost::_bi::value<Vi::Logger*> > >::operator() (this=0x7fffe2c1d890) at /usr/include/boost/bind/bind_template.hpp:20
  11. #10 0x00007ffff6aceff2 in boost::detail::thread_data<boost::_bi::bind_t<void,boost::_bi::list1<boost::_bi::value<Vi::Logger*> > > >::run (this=0x7fffe2c1d760)
  12. at /usr/include/boost/thread/detail/thread.hpp:56
  13. #11 0x00007ffff2cc5230 in thread_proxy () from /usr/lib/libboost_thread.so.1.42.0
  14. #12 0x00007ffff4d87971 in start_thread (arg=<value optimized out>) at pthread_create.c:304
  15. #13 0x00007ffff549c92d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
  16. #14 0x0000000000000000 in ?? ()

在使用unique_lock()然后中断线程的组合中,有可能没有解锁mMutex吗?

解决方法

退出前加入你的主题吗?正如tyz建议的那样,当mutex被销毁时,你的线程仍可以锁定它.

[编辑]

你没有提供可编译和运行的完整示例,很难提供帮助.

检查这个应该与您的类似的简单示例:

  1. #include <boost/thread.hpp>
  2. #include <boost/bind.hpp>
  3. #include <queue>
  4.  
  5. class Test
  6. {
  7. public:
  8. Test()
  9. {
  10. thread = boost::thread(boost::bind(&Test::thread_func,this));
  11. }
  12.  
  13. ~Test()
  14. {
  15. thread.interrupt();
  16. thread.join();
  17. }
  18.  
  19. void run()
  20. {
  21. for (size_t i = 0; i != 10000; ++i) {
  22. boost::lock_guard<boost::mutex> lock(mutex);
  23. queue.push(i);
  24. condition_var.notify_one();
  25. }
  26. }
  27.  
  28. private:
  29. void thread_func()
  30. {
  31. while (!boost::this_thread::interruption_requested())
  32. {
  33. {
  34. boost::unique_lock<boost::mutex> lock(mutex);
  35. while (queue.empty())
  36. condition_var.wait(lock);
  37. queue.pop();
  38. }
  39. }
  40. }
  41.  
  42. private:
  43. boost::thread thread;
  44. boost::mutex mutex;
  45. boost::condition_variable condition_var;
  46. std::queue<int> queue;
  47. };
  48.  
  49. int main()
  50. {
  51. Test test;
  52. test.run();
  53.  
  54. return 0;
  55. }

与你的情况比较

猜你在找的Java相关文章