Solved

How to gracefully close the c++ 11 thread?

Posted on 2016-09-19
3
84 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
3 Comments
 
LVL 61

Expert Comment

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now