How to check if a std::thread is still running in C++
Codemia Knowledge Hub ·
std::thread has no method that tells you whether the thread is still running. The usual workarounds are to launch the work with std::async and poll the returned std::future with wait_for, or to have the thread clear a std::atomic<bool> flag when it finishes. joinable() does not answer the question: it stays true after the thread finishes until you call join() or detach().
The std::thread lifecycle
std::thread was added to the standard library in C++11. A std::thread object represents one thread of execution:
- Creation: the thread starts running the entry function passed to the constructor.
- Running: the thread does its work.
- Completion: the thread finishes when the entry function returns.
- Joinable: the object is joinable from the moment it starts until it is joined or detached, whether or not the function has returned.
- Join or detach: joining makes the current thread wait for it to finish; detaching lets it run independently.
Polling a std::future from std::async
std::async returns a std::future, and wait_for with a short timeout tells you whether the task is done:
wait_for returns std::future_status::ready once the task has completed.
Polling an atomic flag
With a plain std::thread, have the thread clear a std::atomic<bool> when its work ends. The atomic makes the check thread-safe:
What joinable() tells you
joinable() reports whether the thread object still owns a thread that has not been joined or detached. It is the right check before calling join(), but it cannot distinguish a running thread from one that has already finished:
Comparison
| Method | Technique | Thread-Safe | Usage |
std::future with async | Asynchronous task execution | Yes | Use for tasks launched with std::async.
Good for managing future tasks. |
| Polling with a Flag | Atomic flag checking | Yes | Simple implementation for tasks with user-defined running states. |
std::thread::joinable | Checking joinability (indirect) | Yes | Useful for checking before join, but doesn't show running state directly. |
Related reading
- How to check if a thread has finished in Rust?
- How to check if current thread is not main thread
- How to check possibility of deadlock in c code
- How to combine python asyncio with threads?
- How to check node name of tensorflow graph protocol buffers by c
- How to compress pointer ? eg. arbitrary bit pointer
- How to configure a fine tuned thread pool for futures?
- How to consume WinRT IAsyncOperation object in native c environment
System design interview problems on this topic
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough
- Design Ticketmaster: solution and walkthrough
- Design a Flash Sale System: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.