Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to gracefully close the c++ 11 thread?

Posted on 2016-09-19
3
Medium Priority
?
361 Views
Last Modified: 2016-09-23
Hi,

  Created C++ 11 thread by using std::thread. Running in parallel to main program/thread. Now, in some situation, I want to kill or terminate that thread gracefully.

What is the best way to kill child/other threads.

Ed
0
Comment
Question by:EeEd EeEd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 62

Expert Comment

by:gheist
ID: 41806274
When thread function exits thread finishes.
0
 
LVL 35

Accepted Solution

by:
sarabande earned 1000 total points
ID: 41806490
When thread function exits thread finishes.

... gracefully.

therefore, you always should add a way how to return from thread function (and break the - infinite - loop) on request of the main thread. the simplest to do so, is to provide a shared bool variable (stop flag) which was checked by the thread periodically. to stop the thread the main thread would set the bool  and either wait until the thread has recognized the stop flag and exited, or if the results of the thread are no longer needed, simply goes on. the thread may reset the shared stop flag before exit.

Sara
1
 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 1000 total points
ID: 41807786
"Best" way is probably to send some signal that the thread can react to to have it end itself. But that really isn't "kill" a thread. When you "kill" any process, it's almost not "graceful" by definition.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question