Sometimes, we need to kill a thread that has eaten too many resources or that is just executing for too long. For example, some parser works in a thread (and actively uses Boost.Thread
), but we have already got the required amount of data from it, so parsing can be stopped. Here's the stub:
int main() { boost::thread parser_thread(&do_parse); // ... if (stop_parsing) { // No more parsing required. // TODO: Stop the parser! } // ... parser_thread.join(); }
How can we do it?
Almost nothing is required for this recipe. You only need to have at least a basic knowledge of threads.
We can stop a thread by interrupting it:
if (stop_parsing) { // No more parsing required. parser_thread.interrupt(); }