Terminating a Thread

A thread can be terminated in four ways:

  • The thread function returns. (This is highly recommended.)

  • The thread kills itself by calling the ExitThread function. (Avoid this method.)

  • A thread in the same process or in another one calls the TerminateThread function. (Avoid this method.)

  • The process containing the thread terminates. (Avoid this method.)

This section discusses all four methods for terminating a thread and describes what happens when a thread ends.

The Thread Function Returns

You should always design your thread functions so that they return when you want the thread to terminate. This is the only way to guarantee that all your thread’s resources are cleaned up properly.

Having your thread function return ensures the following: ...

Get Windows® via C/C++, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.