Nik Tedig
Nik Tedig

Reputation: 463

Is it possible to return an exit code from a thread in C++?

I'm learning C++ and I'm currently working on a program with two concurrent threads. In the debug window in Visual Studio, it's always telling me that:

The thread x has exited with code 0 (0x0).

I want to return an exit code from my thread so that visual studio can display the exit code in the console like it does with these other threads. I've tested it, and just returning an int from the thread function doesn't change anything about the output of Visual Studio. It still says that the thread in question exited with code 0.

I've also googled it, and I can't find anything about how to do it (at least not with thread.h). It's not a big deal if it isn't possible, because I can always return a value from the thread and mirror it in the process exit code, but if it isn't possible, then what is this exit code stuff I'm seeing in the debug window?

Upvotes: 1

Views: 999

Answers (1)

Alex Guteniev
Alex Guteniev

Reputation: 13699

You'll need to use platform-specific threads instead of std::thread.

In Windows you would use CreateThread. In Visual Studio there is _beginthread and _beginthreadex (use the later only, _beginthead is not recommended), that should be used instead of CreateThread, although currently there's no much difference. When using those functions, you just return exit code from your thread function. Alternatively, call ExitThread or _endthreadex. but this may be not safe, as it will not execute any later code, including destructors of objects in current scope.

Upvotes: 2

Related Questions