In this article, we will discuss how to stop or terminate threads in C++11.
C++11 does not provide a direct method to stop a running thread because some resources of the thread may be released or closed before exiting, i.e
- If a thread obtains a lock and we suddenly kill that thread, who will release the lock?
- If a thread opens a file to write text, and we stop the thread, who will close the file?
- If the thread has allocated memory on the heap and before it can delete the memory, we will stop the thread. So who will prevent memory leakage.
Therefore, there is no function to close the thread directly. However, we can notify the thread to exit. We can implement the out thread in such a way that after a period of time or after some checkpoints, it should check whether it requires me to exit. If so, it should exit gracefully by releasing all resources.
Stop the thread using STD:: future < >
We can pass the STD:: future < void > object to the thread. When the future value is available, the thread should exit. Because we only want to signal the thread, not actually pass any value in the signal, we can use future objects of void type.
Let's create a promise object of void type in the main function, that is
// Create a std::promise object std::promise < void > exitSignal;
Now, get the associated future object from the promise in the main function, that is
//Acquisition and promise Associated std::future object std::future < void > futureObj = exitSignal.get_future () ;
Now in the main function of creating the thread, pass the future object to the thread function, that is
// Start the thread and move by reference lambda Future objects in functions std::thread th ( &threadFunction, std:: move ( futureObj )) ;
Inside the thread, we are doing some work and continue to check whether the thread has been requested to exit, that is, whether future values are available.
void threadFunction(std::future<void> futureObj) { std::cout << "Thread Start" << std::endl; while (futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout) { std::cout << "Doing Some Work" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } std::cout << "Thread End" << std::endl; }
Once we set the value of the promise object from the main function, the value in the future object will be available in the thread function, that is
//Set the value in promise
exitSignal.set_value();
#include <thread> #include <iostream> #include <assert.h> #include <chrono> #include <future> void threadFunction(std::future<void> futureObj) { std::cout << "Thread Start" << std::endl; while (futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout) { std::cout << "Doing Some Work" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } std::cout << "Thread End" << std::endl; } int main() { // Create a std::promise object std::promise<void> exitSignal; //Acquisition and promise Associated std::future object std::future<void> futureObj = exitSignal.get_future(); // Start the thread and move by reference lambda Future objects in functions std::thread th(&threadFunction, std::move(futureObj)); //Wait 10 seconds std::this_thread::sleep_for(std::chrono::seconds(10)); std::cout << "Asking Thread to Stop" << std::endl; //set up promise Values in exitSignal.set_value(); //Wait for the thread to join th.join(); std::cout << "Exiting Main Function" << std::endl; return 0; }
Thread Start
Doing Some Work
Doing Some Work
Doing Some Work
Doing Some Work
Doing Some Work
Doing Some Work
Doing Some Work
Doing Some Work
Doing Some Work
Doing Some Work
Asking Thread to Stop
Thread End
Exiting Main Function