Solved

How to gracefully close the c++ 11 thread?

Posted on 2016-09-19
3
188 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 34

Accepted Solution

by:
sarabande earned 250 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 250 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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