互斥锁解锁异常失败
Mutex unlock fails strangely
我正在玩一些套接字,线程和互斥锁。我的问题是关于线程和互斥锁:
int ConnectionHandler::addNewSocket(){
this->connectionList_mutex.lock();
std::cout << "test1" << std::endl;
this->connectionList_mutex.unlock();
return 0;
}
int ConnectionHandler::main(){
while(true){
this->connectionList_mutex.lock();
std::cout << "test2" << std::endl;
this->connectionList_mutex.unlock();
}
}`
main函数在一个线程中运行,而addNewSocket由另一个线程调用。问题是,当addNewSocket被调用一次(由第二个线程)时,线程1 (main)的下一个解锁将以一个奇怪的"信号SIGABRT"失败。我已经为此工作了两天,但遗憾的是,我没有设法把它修好。我希望你能帮助我。
编辑:ConnectionHandler是一个类,它有connectionList_mutex作为成员。
编辑:有时我也得到这个错误:"断言失败:(ec == 0),函数解锁,文件/SourceCache/libcxx/libcxx-65.1/src/mutex.cpp,第44行。"但它随机发生。
编辑:这是整个类(减少到最低限度,应该在一定程度上独立于上下文,但是当我把它放在客户端连接后立即崩溃,如果我把它放在开始后即可工作:
class ConnectionHandler{
public:
ConnectionHandler();
int addNewSocket();
private:
int main();
static void start(void * pThis);
std::mutex connectionList_mutex;
};
ConnectionHandler::ConnectionHandler(){
std::thread t(&this->start, this);
t.detach();
}
void ConnectionHandler::start(void * pThis){
ConnectionHandler *handlerThis;
handlerThis = (ConnectionHandler *)pThis;
handlerThis->main();
}
int ConnectionHandler::addNewSocket(){
this->connectionList_mutex.lock();
std::cout << "test1" << std::endl;
this->connectionList_mutex.unlock();
return 0;
}
int ConnectionHandler::main(){
while(true){
this->connectionList_mutex.lock();
std::cout << "test2" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
this->connectionList_mutex.unlock();
}
}
我猜您的ConnectionHandler
对象正在某处被销毁。而且,您以一种愚蠢的方式定义了ConnectionHandler::start
。
首先,ConnectionHandler::start
应该这样定义:
void ConnectionHandler::start(ConnectionHandler * pThis){
pThis->main();
}
c++ 11 ::std::thread
类完全能够保留函数参数的类型,因此没有必要诉诸void *
。
void ConnectionHandler::~ConnectionHandler(){
const void * const meptr = this;
this->connectionList_mutex.lock();
::std::cout << "ConnectionHandler being destroyed at " << meptr << ::std::endl;
this->connectionList_mutex.unlock();
}
并将构造函数改为:
ConnectionHandler::ConnectionHandler(){
const void * const meptr = this;
::std::cout << "ConnectionHandler being created at " << meptr << ::std::endl;
std::thread t(&this->start, this);
t.detach();
}
这将显示ConnectionHandler
对象何时被销毁。我的猜测是,你的代码正在销毁它,而你的分离线程仍在运行。
meptr
的事情是因为operator <<
对void *
有一个重载,输出指针值。如果要创建多个ConnectionHandler
对象,那么打印出this
的指针值将允许您匹配对构造函数和析构函数的调用。
编辑:既然事实证明我是正确的,以下是我建议你如何编写游戏ConnectionHandler类:
#include <iostream>
#include <atomic>
#include <thread>
#include <chrono>
#include <mutex>
class ConnectionHandler {
public:
ConnectionHandler();
~ConnectionHandler();
ConnectionHandler(const ConnectionHandler &) = delete;
const ConnectionHandler &operator =(const ConnectionHandler &) = delete;
int addNewSocket();
private:
int main();
static void start(ConnectionHandler * pThis);
::std::mutex connectionList_mutex;
volatile ::std::atomic_bool thread_shutdown;
::std::thread thread;
};
ConnectionHandler::ConnectionHandler()
: thread_shutdown(false), thread(&this->start, this)
{
}
ConnectionHandler::~ConnectionHandler()
{
thread_shutdown.store(true);
thread.join();
}
void ConnectionHandler::start(ConnectionHandler * pThis){
pThis->main();
}
int ConnectionHandler::addNewSocket(){
::std::lock_guard< ::std::mutex> lock(connectionList_mutex);
::std::cout << "test1" << ::std::endl;
return 0;
}
int ConnectionHandler::main(){
while(!thread_shutdown.load()){
::std::lock_guard< ::std::mutex> lock(connectionList_mutex);
::std::cout << "test2" << ::std::endl;
::std::this_thread::sleep_for(::std::chrono::milliseconds(100));
}
return 0;
}
- 为什么此代码导致未处理的异常失败
- 使用双LL在C++中实现Stack失败,出现异常"EXC_BAD_ACCESS(代码=2,地址=0x7fff5
- 如何在使用 CFileDialog 打开文件失败时捕获异常
- 声纳库贝分析在 Eclipse 中失败,由于找不到异常语言"cpp"在本地分析中抛出错误
- 运行失败,没有任何异常
- 如何处理失败的方法:通过使用异常或使方法返回bool
- 捕获派生异常类型在 Clang/MacOS X 上失败
- 'new'语句是否可以在不引发异常的情况下失败?
- C++ 面向对象的编程异常失败
- vHRESULT 中未处理的异常失败
- C++自做异常处理失败
- 单元测试失败,异常代码为c0000005
- 异常类使用递归函数失败
- 程序在Turbo C中运行良好,但在MSVC中失败,出现未处理的异常
- 可能失败的函数的接收参数和移动语义(强异常安全性)
- 如何检查禁用异常的构造函数的失败
- 捕获异常:error: svd()收敛失败
- 试图在递归函数内部捕获失败的分配:未处理的异常/堆栈溢出
- c++异常处理失败
- 互斥锁解锁异常失败