C ++-执行时std :: thread崩溃 [英] C++ - std::thread crashes upon execution

查看:786
本文介绍了C ++-执行时std :: thread崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2012,如果没有崩溃,我将无法在程序中执行线程.应该注意的是,我的程序包含OpenGL和SOIL.

I am using VS2012 and I can't execute a thread in my program without it crashing. It should be noted that my program contains OpenGL and 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);
}

程序调用connectToServer()时,它将在具有以下调用堆栈的调用语句std::thread serverConnect(service);处中断:

When the program calls connectToServer() it breaks at the call statement 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

该程序无需线程调用语句即可完美运行.另外,我的VS环境在运行像这样的简单示例线程程序时没有问题:

The program works perfectly without the thread call statement. Also, my VS environment has no problem running simple example thread programs like this one:

#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.

推荐答案

销毁与joinable()线程关联的std::thread对象会导致调用std::terminate(). §30.3.1.3[thread.thread.destr]:

Destroying a std::thread object associated with a joinable() thread causes std::terminate() to be called. §30.3.1.3 [thread.thread.destr]:

~thread();

如果为joinable(),则调用std::terminate().否则,没有 效果. [注意:隐式分离或加入joinable() 析构函数中的线程可能导致难以调试 遇到的正确性(用于分离)或性能(用于连接)的错误 仅在引发异常时.因此,程序员必须确保 当线程仍可连接时,永远不会执行析构函数. — 尾注]

If joinable(), calls 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. 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:

  • 在堆上分配线程,并让您的函数返回指向该线程对象的智能指针
  • 或者让它返回std::thread(将serverConnect移至返回值)
  • serverConnect移动到connectToServer()返回时不会被破坏的内容(例如全局变量)
  • join()返回之前的线程
  • detach()返回之前的线程
  • 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)
  • Move serverConnect into something that won't be destroyed when connectToServer() returns (e.g., a global variable)
  • join() the thread before you return
  • detach() the thread before you return

正确的选择取决于您的特定用例.

The correct choice depends on your particular use case.

这篇关于C ++-执行时std :: thread崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆