Jump to content
  • Advertisement

Recommended Posts

Given code

std::thread * st = new std::thread(std::bind(&Main::SrvThread, this));
progress_frm.tuid =st;
st->detach();

How one would terminate it, delete st; doesnt stop the thread at all.

Note that void SrvThread()

{

Uses some nested loops and i really try to avoid checking for break/return condition like

while (!finished)

while (sth1) {

If (finished) return;

While(sth2) if (finished) return;

}

}

Share this post


Link to post
Share on other sites
Advertisement

If you detach the thread it should end itself when the function returns.  By 'end itself' I mean it may go into a zombie state until the OS cleans it up, but detaching means you no longer have any control over it from the spawning thread.

If you really want to clean up after it, don't detach it, join it instead.

Share this post


Link to post
Share on other sites

Killing a thread from another thread has existed in the early days, but it is very hazardous to do so. Since you don't know what the killed thread was doing, you cannot ensure it leaves everything in a sane state. For example if it was in a critical section at the time of the kill, you made the resource that is protected by that section unavailable for everybody!!

Since this is not solvable, the ability to stop a thread has been removed.

To stop a thread, you can do 3 things.

1) shutdown the computer

2) end the entire program at OS level (a crash will do the trick nicely)

3) send the thread that should stop a message that its services are no longer needed, and it should clean up and stop.

Such messages are sent in the same way you communicate between threads normally. A thread can stop itself by ending the function it is running in, as others have already mentioned..

Share this post


Link to post
Share on other sites
12 hours ago, Gnollrunner said:

Lookup "join". Use it from the thread you are leaving.

I just realized I worded this poorly. You should call it as a member function of the thread you are leaving, but from some other thread, like your main thread.

Share this post


Link to post
Share on other sites

Windows API still supports shutdown of a thread created by it but that leads to an exception more often it helps. The only other chance you have is to define certain checking points in your thread function and as mentioned, join the thread until it has reached such an exit point to terminate itself. There is no other option especially when using STL!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!