C++ - std::thread crashes upon execution

c++, multithreading, opengl

Solution

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

~thread();

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:

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

Problem

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); } ``` 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 ``` 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.

Original source