互斥锁解锁异常失败

Mutex unlock fails strangely

本文关键字:异常 失败 解锁      更新时间:2023-10-16

我正在玩一些套接字,线程和互斥锁。我的问题是关于线程和互斥锁:

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;
}