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

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(主线程)进行的下一次解锁将失败,并带有一个奇怪的“signal 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 *的重载,该重载会打印出指针值。如果创建多个this对象,则打印出ConnectionHandler的指针值将使您可以将对构造函数和析构函数的调用进行匹配。

编辑:事实证明我是正确的,这是我建议您编写Play 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;
}

关于c++ - 互斥锁解锁失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14068816/

10-11 15:53