If you need to kill a thread, consider using a process.
Especially if you tell us that your "thread" is a while (true) loop that can sleep for a long time, performing operations that are necessarily blocked. For me, this indicates the behavior of the process.
Processes can be terminated in a variety of ways at almost any time and always clean. They can also provide greater reliability in the event of a failure.
Modern operating systems offer many interprocess communication tools: sockets, channels, shared memory, memory mapped files ... They can even exchange file descriptors.
Good OSs have a write-to-write mechanism, so processes are cheap for fork.
Please note that if your operations can be performed in a non-blocking way, then you should use a poll-like mechanism. Boost :: asio can help there.
source share