Search code examples
c++multithreadingwinsock2

Should I use ExitThread() or Return from threads in C++


I'm developing a server app to which multiple clients will connect. For every new client, I create a new thread and want to free all the resources of each client once the client disconnect.

My main thread doesn't need return values from client(s) so I just want to safely terminate threads and deallocate the resources uses by/in the thread.

What should I use in this case and why ?


Solution

  • Does this answer your question? Sometimes it is very useful to read the reference:

    ExitThread

    Remarks

    ExitThread is the preferred method of exiting a thread in C code. However, in C++ code, the thread is exited before any destructors can be called or any other automatic cleanup can be performed. Therefore, in C++ code, you should return from your thread function.