2024-01-07 How to stop Linux threads cleanly Let’s say you’re writing a long running multi-threaded application, on Linux. Maybe it’s a database or a server of some sort. Let’s also imagine that you’re not running on some managed runtime (maybe the JVM, Go, or BEAM), but rather managing threads spawned using the clone syscall. Think of threads created in C with pthread_create, or using C++’s std::thread. Once you get into the business of starting threads, you’re probably also in the business of stopping them. However the former is much easier than the latter. With “stopping” I mean stopping the thread while giving it a chance to run some cleanup operations before fully terminating. Or in other words, we want to terminate a thread while ensuring that memory is freed, locks are released, logs are flushed, and so on. This task is sadly not as straightforward as it should be, and there definitely isn’t a one-size-fits-all solution. This blog post aims to give an overview of the problem space and to highlight some pitfalls in an area with no shortage, and present a little magic trick at the end. (Quasi-)busy looping # If you can afford it, you can structure each thread as such: while (true) { if (stop) { break; } // Perform some work completing in a reasonable time } stop here is a per-thread boolean. When we want to stop a thread, we set stop to true, and then call pthread_join or equivalents to ensure that the thread has actually terminated. Here’s a contrived but working example in C++: #include <thread> #include <atomic> #include <stdio.h> #include <unistd.h> static std::atomic<bool> stop = false; int main() { std::thread thr([] { // prints every second until stopped for (int i = 0; !stop.load(); i++) { printf("iterated %d times\n", i); sleep(1); } printf("thread terminating\n"); }); // waits 5 seconds, then stops thread sleep(5); stop.store(true); thr.join(); printf("thread terminated\n"); return 0; } Which prints: iterated 0 times iterated 1 times iterated 2 times ...
First seen: 2025-10-20 16:05
Last seen: 2025-10-21 13:09