
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How do I terminate a thread in C++11?
Here we will see, how to terminate the threads in C++11. The C++11 does not have direct method to terminate the threads.
The std::future<void> can be used to the thread, and it should exit when value in future is available. If we want to send a signal to the thread, but does not send the actual value, we can pass void type object.
To create one promise object, we have to follow this syntax −
std::promise<void> exitSignal;
Now fetch the associated future object from this created promise object in main function −
std::future<void> futureObj = exitSignal.get_future();
Now pass the main function while creating the thread, pass the future object −
std::thread th(&threadFunction, std::move(futureObj));
Example
#include <thread> #include <iostream> #include <assert.h> #include <chrono> #include <future> using namespace std; void threadFunction(std::future<void> future){ std::cout << "Starting the thread" << std::endl; while (future.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout){ std::cout << "Executing the thread....." << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(500)); //wait for 500 milliseconds } std::cout << "Thread Terminated" << std::endl; } main(){ std::promise<void> signal_exit; //create promise object std::future<void> future = signal_exit.get_future();//create future objects std::thread my_thread(&threadFunction, std::move(future)); //start thread, and move future std::this_thread::sleep_for(std::chrono::seconds(7)); //wait for 7 seconds std::cout << "Threads will be stopped soon...." << std::endl; signal_exit.set_value(); //set value into promise my_thread.join(); //join the thread with the main thread std::cout << "Doing task in main function" << std::endl; }
Output
Starting the thread Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Executing the thread..... Threads will be stopped soon.... Thread Terminated Doing task in main function
- Related Articles
- How to execute a script when I terminate a Docker container?
- How to terminate a script in JavaScript?
- How to terminate javascript forEach()?
- How to terminate a MongoDB shell script earlier?
- What exactly does a Thread count do in TestNG?
- How a thread can interrupt another thread in Java?
- How can I display Toast messages from a thread in Android using Kotlin?
- Terminate the timer in Java
- How to get the thread ID from a thread in C#?
- Do you think a Python dictionary is thread safe?
- How to terminate/destroy a process using Process API in Java 9?
- How do I do a case insensitive string comparison in Python?
- How do I copy a file in python?
- How do I declare a namespace in JavaScript?
- How do I create a view in MySQL?

Advertisements