简体   繁体   English

C++ - std::thread 在执行时崩溃

[英]C++ - std::thread crashes upon execution

I am using VS2012 and I can't execute a thread in my program without it crashing.我正在使用 VS2012,我无法在程序中执行线程而不崩溃。 It should be noted that my program contains OpenGL and SOIL.应该注意的是,我的程序包含 OpenGL 和 SOIL。

I simply call a blank thread, a function with no statements, in one of my functions and it immediately crashes:我只是在我的一个函数中调用一个空白线程,一个没有语句的函数,它立即崩溃:

void service(){

}

/* Connect to server */
void connectToServer(){

    cout << "~CLIENT~\n" << endl;

    std::thread serverConnect(service);
}

When the program calls connectToServer() it breaks at the call statement std::thread serverConnect(service);当程序调用connectToServer()它在调用语句std::thread serverConnect(service); with the following call-stack:具有以下调用堆栈:

msvcr110.dll!_crt_debugger_hook(int _Reserved) Line 60  C
msvcr110.dll!_call_reportfault(int nDbgHookCode, unsigned long dwExceptionCode, unsigned long dwExceptionFlags) Line 152    C++
msvcr110.dll!abort() Line 90    C
msvcr110.dll!terminate() Line 96    C++
IRC.exe!connectToServer() Line 449  C++
IRC.exe!handleKeypress(unsigned char key, int x, int y) Line 936    C++
glut32.dll!1000e054()   Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for glut32.dll]    
glut32.dll!1000d5de()   Unknown
user32.dll!753962fa()   Unknown
user32.dll!75396d3a()   Unknown
user32.dll!75396ce9()   Unknown
user32.dll!753a0d27()   Unknown
user32.dll!753a0d4d()   Unknown
opengl32.dll!18f160fb() Unknown
user32.dll!753962fa()   Unknown
user32.dll!75396d3a()   Unknown
user32.dll!75396ce9()   Unknown
user32.dll!753977c4()   Unknown
user32.dll!753bd62a()   Unknown
user32.dll!75397bca()   Unknown
glut32.dll!10004970()   Unknown
glut32.dll!10004a7a()   Unknown
glut32.dll!1000491f()   Unknown
IRC.exe!main(int argc, char * * argv) Line 1683 C++
IRC.exe!__tmainCRTStartup() Line 536    C
kernel32.dll!7551338a() Unknown
ntdll.dll!77049f72()    Unknown
ntdll.dll!77049f45()    Unknown

The program works perfectly without the thread call statement.该程序在没有线程调用语句的情况下完美运行。 Also, my VS environment has no problem running simple example thread programs like this one:此外,我的 VS 环境运行这样的简单示例线程程序没有问题:

#include <iostream>
#include <thread>
using namespace std;
//This function will be called from a thread

void call_from_thread() {
    std::cout << "Hello, World" << std::endl;
}

int main() {
    //Launch a thread
    thread t1(call_from_thread);

    system("pause");
    return 0;
}

It's only when I use threads in my program that it crashes.只有当我在我的程序中使用线程时它才会崩溃。

Destroying a std::thread object associated with a joinable() thread causes std::terminate() to be called.销毁与joinable()线程关联的std::thread对象会导致调用std::terminate() §30.3.1.3 [thread.thread.destr]: §30.3.1.3 [thread.thread.destr]:

 ~thread();

If joinable() , calls std::terminate() .如果joinable() ,则调用std::terminate() Otherwise, has no effects.否则,没有任何影响。 [ Note : Either implicitly detaching or joining a joinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. [注意:在其析构函数中隐式分离或加入joinable()线程可能导致难以调试正确性(对于分离)或仅在引发异常时遇到性能(对于连接)错误。 Thus the programmer must ensure that the destructor is never executed while the thread is still joinable.因此,程序员必须确保在线程仍可连接时永远不会执行析构函数。 end note ] 尾注]

There are a multitude of possible fixes:有多种可能的修复方法:

  • Allocate the thread on the heap and having your function return a smart pointer to the thread object在堆上分配线程并让您的函数返回一个指向线程对象的智能指针
  • Or have it return a std::thread (move serverConnect into the return value)或者让它返回一个std::thread (将serverConnect移动到返回值中)
  • Move serverConnect into something that won't be destroyed when connectToServer() returns (eg, a global variable)serverConnect移动到当connectToServer()返回时不会被破坏的东西中(例如,全局变量)
  • join() the thread before you return在返回之前join()线程
  • detach() the thread before you return在返回之前detach()线程

The correct choice depends on your particular use case.正确的选择取决于您的特定用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM