ZOl*_*ier 0 c++ windows multithreading stdthread c++17
抱歉,标题是一个点击诱饵...解决起来并不像您想象的那么容易...这是一个真正的挑战
我遇到一个非常奇怪的问题,即 joinable() 的线程无法 join()。
我得到的错误是No such process。
这不是典型的初学者两次加入线程的错误...这是一个复杂的问题,甚至可能是由内存损坏引起的...但我希望我只是错过了一些东西,我需要一个新的外部视图...我已经研究这个问题两天了。
我正在为 Linux 和 Windows 进行编译。
在 Linux 上(使用 gcc 9.1.0),它每次都能完美运行。
在 Windows 上(在我的 Linux 机器上使用 x86_64-w64-mingw32-g++ 9.2.0 并在我的 Windows 机器上运行该程序)我总是收到错误。
以下是我可以 100% 确认的内容:
最后一点很可能是问题的根源,尽管我真的不明白是怎么回事。
我还知道包含线程指针的对象在 join() 之前不会被销毁。如果成功的话,我删除该指针的唯一位置是在 join() 之后。父对象被包装在一个shared_ptr 中。
指向该线程的指针也从未在其他地方使用/共享。
该代码在这里很难简化和共享,因为它是完整网络系统的一部分,并且它的所有方面都可能是问题的根源。
哦,实际的线程已正确执行,并且所有生成的网络通信都按其应有的方式工作,即使该线程无法加入。
这是重要部分的非常简化的版本,并附有解释所发生情况的注释:
// We instantiate a new ListeningServer then call Start(),
// then we connect a client to it, we transfer some data,
// then we call Stop() on the ListeningServer and we get the error, but everything worked flawlessly still
typedef std::function<void(std::shared_ptr<ListeningSocket>)> Func;
class ListeningServer {
ListeningSocket listeningSocket; // The class' Constructor initializes it correctly
void Start(uint16_t port) {
listeningSocket.Bind(port);
listeningSocket.StartListeningThread([this](std::shared_ptr<ListeningSocket> socket) {
HandleNewConnection(socket);
});
}
void HandleNewConnection(std::shared_ptr<ListeningSocket> socket) {
// Whatever we are doing here works flawlessly and does not change the outcome of the error
}
void Stop() {
listeningSocket.Disconnect();
}
};
class ListeningSocket {
SOCKET socket = INVALID_SOCKET; // Native winsock fd handle for windows or typedefed to int on linux
std::thread* listeningThread = nullptr;
std::atomic<bool> listening = false;
void StartListeningThread(Func&& newSocketCallback) {
listening = (::listen(socket, SOMAXCONN) >= 0);
if (!listening) return; // That does not happen, we're still good
listeningThread = new std::thread([this](std::shared_ptr<ListeningSocket>&& newSocketCallback){
while (IsListening()) {
// Here I have Ommited a ::poll call with a 10ms timeout as interval so that the thread does not block, the issue is happening with or without it
memset(&incomingAddr, 0, sizeof(incomingAddr));
SOCKET clientSocket = ::accept(socket, (struct sockaddr*)&incomingAddr, &addrLen);
if (IsListening() && IsValid(clientSocket)) {
newSocketCallback(std::make_shared<ClientSocket>(clientSocket, incomingAddr)); // ClientSocket is a wrapper to native SOCKET with addr info and stuff...
}
}
LOG("ListeningThread Finished") // This is correctly logged just before the error
}, std::forward<Func>(newSocketCallback));
LOG("Listening with Thread " << listeningThread->get_id()) // This is correctly logged to the same thread id that we want to join() after
}
INLINE void Disconnect() {
listening = false; // will make IsListening() return false
if (listeningThread) {
if (listeningThread->joinable()) {
LOG("*** Socket Before join thread " << listeningThread->get_id()) // Logs the correct thread id
try {
listeningThread->join();
delete listeningThread;
listeningThread = nullptr;
LOG("*** Socket After join thread") // NEVER LOGGED
} catch(...) {
LOG("JOIN ERROR") // it ALWAYS goes here with "No Such Process"
SLEEP(100ms) // We need to make sure the thread still finishes in time
// The thread finishes in time and all resulting actions work flawlessly
}
}
}
#ifdef _WINDOWS
::closesocket(socket);
#else
::close(socket);
#endif
socket = INVALID_SOCKET;
}
};
Run Code Online (Sandbox Code Playgroud)
需要注意的另一件重要的事情是,在程序的其他地方,我直接实例化 ListeningSocket 并使用 lambda 调用 StartListeningThread() ,并且在直接调用 Disconnect() 后不会失败加入线程
此外,部分代码是在动态链接的共享库中编译的。
问题解决了!
看起来,仅在 Windows 中,人们无法从共享库中编译的代码创建线程并尝试从主应用程序中编译的代码加入该线程。
基本上, joinable() 将返回 true,但 .join() 或 .detach() 将失败。
我所要做的就是确保线程是从最初在同一文件中编译的代码创建和连接的。
当我提出问题时,这就是我正在寻找的提示,因为我知道它比这更复杂,并且简化的最小代码将无法重现该问题。
Windows 中线程的这种约束没有在任何地方记录(据我所知,并且我搜索过),因此很可能它不应该是一个约束,实际上是我正在使用的编译器中的一个错误。
| 归档时间: |
|
| 查看次数: |
1613 次 |
| 最近记录: |