Mutex解锁失败了

时间:2012-12-28 11:28:07

标签: c++ xcode multithreading mutex std

我正在玩一些套接字,线程和互斥。我的问题涉及线程和互斥体:

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

}`

主函数在一个线程中运行,而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();

    }

}

1 个答案:

答案 0 :(得分:5)

我的猜测是你的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 *的重载,它打印出指针值。如果要创建多个this对象,则打印出ConnectionHandler的指针值将允许您匹配对构造函数和析构函数的调用。

编辑:既然事实证明我是对的,这就是我建议您编写PlayHandler类的方法:

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