Home >Backend Development >C++ >How Can I Efficiently Check the Status of a Running std::thread in C ?
Checking the Status of a Running std::thread
In C , std::thread is a type of class for implementing concurrency. It can be challenging to determine whether a std::thread is still running, especially if platform independence is crucial.
Originally, std::thread lacked a timed_join() method, and joinable() was not intended for this purpose. An alternative solution is proposed that utilizes std::lock_guard to lock a mutex within the thread and then uses the try_lock() method to assess if it is still locked, indicating the thread's running state. However, this strategy is considered unnecessarily complex.
Elegant Solutions for Checking Thread Status
For a cleaner approach, consider leveraging std::async and std::future. std::async enables asynchronous tasks on a separate thread, and std::future allows for retrieving the results of the operation. The wait_for function of std::future can be used with a zero milliseconds timeout to effectively check if the thread is still running:
#include <future> #include <thread> #include <chrono> #include <iostream> int main() { // Create an asynchronous task on a new thread using std::async. auto future = std::async(std::launch::async, [] { std::this_thread::sleep_for(3s); return 8; }); // Check thread status using wait_for() with zero milliseconds. auto status = future.wait_for(0ms); // Print status according to the wait_for() result. if (status == std::future_status::ready) { std::cout << "Thread finished" << std::endl; } else { std::cout << "Thread still running" << std::endl; } auto result = future.get(); // Retrieve result. }
Alternatively, one can use std::promise to obtain a future object from a std::thread:
#include <future> #include <thread> #include <chrono> #include <iostream> int main() { // Create a promise and its associated future. std::promise<bool> p; auto future = p.get_future(); // Run a task on a new thread using std::thread. std::thread t([&p] { std::this_thread::sleep_for(3s); p.set_value(true); // Set the promise value atomically. }); // Check thread status using wait_for() as previous example. auto status = future.wait_for(0ms); // Print status according to the wait_for() result. if (status == std::future_status::ready) { std::cout << "Thread finished" << std::endl; } else { std::cout << "Thread still running" << std::endl; } t.join(); // Join the thread. }
Both examples will initially display "Thread still running" because the status is checked before the thread has completed. However, an even simpler solution is to utilize an atomic boolean flag:
#include <thread> #include <atomic> #include <chrono> #include <iostream> int main() { // Use an atomic boolean flag for thread status tracking. std::atomic<bool> done(false); // Run a task on a new thread that sets `done` to true when finished. std::thread t([&done] { std::this_thread::sleep_for(3s); done = true; }); // Check thread status using atomic flag. if (done) { std::cout << "Thread finished" << std::endl; } else { std::cout << "Thread still running" << std::endl; } t.join(); // Join the thread. }
The above is the detailed content of How Can I Efficiently Check the Status of a Running std::thread in C ?. For more information, please follow other related articles on the PHP Chinese website!