作为项目的一部分,我正在编写记录器功能.当程序想要记录某些内容时,此记录器功能会发送电子邮件.由于SMTP服务器没有响应,我决定在单独的线程中发送邮件.
该线程从std :: deque中读取消息,该消息由日志记录函数填充.
线程设置如下:
该线程从std :: deque中读取消息,该消息由日志记录函数填充.
线程设置如下:
- while (!boost::this_thread::interruption_requested())
- {
- EmailItem emailItem;
- {
- boost::unique_lock<boost::mutex> lock(mMutex);
- while (mEmailBuffer.empty())
- mCond.wait(lock);
- bufferOverflow = mBufferOverflow;
- mBufferOverflow = false;
- nrOfItems = mEmailBuffer.size();
- if (nrOfItems > 0)
- {
- emailItem = mEmailBuffer.front();
- mEmailBuffer.pop_front();
- }
- }
- if (nrOfItems > 0)
- {
- bool sent = false;
- while(!sent)
- {
- try
- {
- ..... Do something with the message .....
- {
- boost::this_thread::disable_interruption di;
- boost::lock_guard<boost::mutex> lock(mLoggerMutex);
- mLogFile << emailItem.mMessage << std::endl;
- }
- sent = true;
- }
- catch (const std::exception &e)
- {
- // Unable to send mail,an exception occurred. Retry sending it after some time
- sent = false;
- boost::this_thread::sleep(boost::posix_time::seconds(LOG_WAITBEFORE_RETRY));
- }
- }
- }
- }
函数log()向deque(mEmailBuffer)添加一条新消息,如下所示:
- {
- boost::lock_guard<boost::mutex> lock(mMutex);
- mEmailBuffer.push_back(e);
- mCond.notify_one();
- }
当主程序退出时,将调用记录器对象的析构函数.这是它出错的地方,应用程序崩溃并出现错误:
- /usr/include/boost/thread/pthread/mutex.hpp:45: boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&m)' Failed.
- mQueueThread.interrupt();
- mQueueThread.join();
在主程序中,我使用了多个不同的类,它们也使用了boost线程和互斥,这会导致这种行为吗?不调用logger对象的析构函数不会导致错误,就像使用logger对象而不执行任何其他操作一样.
我的猜测是我做了一些非常错误的事情,或者当使用多个线程划分多个线程时,线程库中存在一个错误.
有谁知道这个错误的原因是什么?
编辑:
我做了@Andy T提议并尽可能地删除了代码.我删除了在不同线程中运行的函数中的几乎所有内容.线程现在看起来像:
- void Vi::Logger::ThreadedQueue()
- {
- bool bufferOverflow = false;
- time_t last_overflow = 0;
- unsigned int nrOfItems = 0;
- while (!boost::this_thread::interruption_requested())
- {
- EmailItem emailItem;
- // Check for new log entries
- {
- boost::unique_lock<boost::mutex> lock(mMutex);
- while (mEmailBuffer.empty())
- mCond.wait(lock);
- }
- }
- }
- #0 0x00007ffff53e9ba5 in raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
- #1 0x00007ffff53ed6b0 in abort () at abort.c:92
- #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
- #3 0x00007ffff7b930f3 in boost::mutex::lock (this=0x7fffe2c1b0b8) at /usr/include/boost/thread/pthread/mutex.hpp:50
- #4 0x00007ffff7b9596c in boost::unique_lock<boost::mutex>::lock (this=0x7fffe48b3b40) at /usr/include/boost/thread/locks.hpp:349
- #5 0x00007ffff7b958db in boost::unique_lock<boost::mutex>::unique_lock (this=0x7fffe48b3b40,m_=...) at /usr/include/boost/thread/locks.hpp:227
- #6 0x00007ffff6ac2bb7 in Vi::Logger::ThreadedQueue (this=0x7fffe2c1ade0) at /data/repos_ViNotion/stdcomp/Logging/trunk/src/Logger.cpp:198
- #7 0x00007ffff6acf2b2 in boost::_mfi::mf0<void,Vi::Logger>::operator() (this=0x7fffe2c1d890,p=0x7fffe2c1ade0) at /usr/include/boost/bind/mem_fn_template.hpp:49
- #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
- #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
- #10 0x00007ffff6aceff2 in boost::detail::thread_data<boost::_bi::bind_t<void,boost::_bi::list1<boost::_bi::value<Vi::Logger*> > > >::run (this=0x7fffe2c1d760)
- at /usr/include/boost/thread/detail/thread.hpp:56
- #11 0x00007ffff2cc5230 in thread_proxy () from /usr/lib/libboost_thread.so.1.42.0
- #12 0x00007ffff4d87971 in start_thread (arg=<value optimized out>) at pthread_create.c:304
- #13 0x00007ffff549c92d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
- #14 0x0000000000000000 in ?? ()
在使用unique_lock()然后中断线程的组合中,有可能没有解锁mMutex吗?
解决方法
你退出前加入你的主题吗?正如tyz建议的那样,当mutex被销毁时,你的线程仍可以锁定它.
[编辑]
你没有提供可编译和运行的完整示例,很难提供帮助.
检查这个应该与您的类似的简单示例:
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <queue>
- class Test
- {
- public:
- Test()
- {
- thread = boost::thread(boost::bind(&Test::thread_func,this));
- }
- ~Test()
- {
- thread.interrupt();
- thread.join();
- }
- void run()
- {
- for (size_t i = 0; i != 10000; ++i) {
- boost::lock_guard<boost::mutex> lock(mutex);
- queue.push(i);
- condition_var.notify_one();
- }
- }
- private:
- void thread_func()
- {
- while (!boost::this_thread::interruption_requested())
- {
- {
- boost::unique_lock<boost::mutex> lock(mutex);
- while (queue.empty())
- condition_var.wait(lock);
- queue.pop();
- }
- }
- }
- private:
- boost::thread thread;
- boost::mutex mutex;
- boost::condition_variable condition_var;
- std::queue<int> queue;
- };
- int main()
- {
- Test test;
- test.run();
- return 0;
- }
与你的情况比较