gpt4 book ai didi

c++ - 互斥锁解锁异常失败

转载 作者:可可西里 更新时间:2023-11-01 18:37:06 31 4
gpt4 key购买 nike

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

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(主线程)的下一次解锁将失败并出现一个奇怪的“信号 SIGABRT”。我已经为此工作了两天,但遗憾的是我没能修好它。我希望你能帮助我。

编辑:ConnectionHandler 是一个类,它的成员是 connectionList_mutex。

编辑:有时我也会收到此错误:“断言失败:(ec == 0),函数解锁,文件/SourceCache/libcxx/libcxx-65.1/src/mutex.cpp,第 44 行。”但它是随机发生的。

编辑:这是整个类(class)(减少到最低限度,应该在一定程度上独立于上下文,但是当我在客户端连接后立即放置它时崩溃,如果我在开始后立即放置它则工作:

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 class 完全能够保留函数参数的类型,因此无需求助于 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com