1

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?

Nik Tedig
  • 453
  • 2
  • 6
  • No, there is nothing like that in standard C++. The C++ library has a separate promise/future template objects that you can use to implement this functionality. – Sam Varshavchik Dec 06 '20 at 16:29
  • @SamVarshavchik I don't know exactly how StackOverflow works, but are you the one who closed this post? Because my question is specifically about returning exit codes, not just general return values. Also, I'm still waiting on an answer to my last question in the post, which has nothing to do with the linked post. I don't think this post deserves to be closed, could you open it back up? – Nik Tedig Dec 06 '20 at 16:43
  • void ExitThread( DWORD dwExitCode ); read docs first ! https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-exitthread – engf-010 Dec 06 '20 at 17:17
  • Windows is not the only platform. In Linux, for example, there is no such thing as a thread exit code. That's why the standard has no notion of thread exit code. `std::thread` can invoke `void` functions, and when your function returns anything but `void`, the return value is simply discarded. – Evg Dec 06 '20 at 18:26
  • @Evg Although it's correct that in Linux thread doesn't have an "exit code", the return value of a thread is a `void*` instead which can be used to represent and exit code. – eerorika Dec 06 '20 at 18:29
  • @eerorika As far as I remember, that `void*` cannot take an arbitrary value - there are some reserved values, and to return an arbitrary value one would have to allocate it on heap and return a pointer to it instead of just doing `return reinterpret_cast(ret);`. – Evg Dec 06 '20 at 18:36
  • @Evg As far as C++ standard is concerned, the mapping of such reinterpretation cast is implementation defined. In practice, I haven't yet encountered an implementation where converting an integer to pointer and back would not keep the original value. Even if that wasn't the case, it is reasonable to allocate the integer on the stack of the thread that joins the returning thread. No heap required. – eerorika Dec 06 '20 at 19:09
  • @eerorika That limitation comes from the Linux itself, not from the C++ standard - not an arbitrary pointer value can be returned. And if a return value is allocated on stack, I guess no return value `void*` is needed: we already know the address. Instead, we'll need to pass that address as an additional argument to the thread function. – Evg Dec 06 '20 at 19:23

1 Answers1

2

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.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79