C++
std::thread
multithreading
concurrency
programming tips

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:

  1. Creation: the thread starts running the entry function passed to the constructor.
  2. Running: the thread does its work.
  3. Completion: the thread finishes when the entry function returns.
  4. Joinable: the object is joinable from the moment it starts until it is joined or detached, whether or not the function has returned.
  5. 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:

cpp
1#include <iostream>
2#include <thread>
3#include <future>
4#include <chrono>
5
6void longTask() {
7    std::this_thread::sleep_for(std::chrono::seconds(2));
8}
9
10int main() {
11    std::future<void> fut = std::async(std::launch::async, longTask);
12
13    while (fut.wait_for(std::chrono::milliseconds(100)) != std::future_status::ready) {
14        std::cout << "Thread is still running...\n";
15    }
16
17    std::cout << "Thread has finished.\n";
18    return 0;
19}

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:

cpp
1#include <iostream>
2#include <thread>
3#include <atomic>
4#include <chrono>
5
6std::atomic<bool> running(true);
7
8void longTask() {
9    std::this_thread::sleep_for(std::chrono::seconds(2));
10    running = false;
11}
12
13int main() {
14    std::thread t(longTask);
15
16    while (running) {
17        std::cout << "Thread is still running...\n";
18        std::this_thread::sleep_for(std::chrono::milliseconds(100));
19    }
20
21    t.join();
22    std::cout << "Thread has finished.\n";
23    return 0;
24}

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:

cpp
1#include <iostream>
2#include <thread>
3
4void longTask() {
5    std::this_thread::sleep_for(std::chrono::seconds(2));
6}
7
8int main() {
9    std::thread t(longTask);
10
11    if (t.joinable()) {
12        std::cout << "Thread is working...\n";
13    }
14
15    t.join();
16    std::cout << "Thread has completed its work.\n";
17    return 0;
18}

Comparison

MethodTechniqueThread-SafeUsage
std::future with asyncAsynchronous task executionYesUse for tasks launched with std::async. Good for managing future tasks.
Polling with a FlagAtomic flag checkingYesSimple implementation for tasks with user-defined running states.
std::thread::joinableChecking joinability (indirect)YesUseful for checking before join, but doesn't show running state directly.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions