Boost.Thread: Is there any way I can kill a running thread?

Started by
5 comments, last by Mercenarey 16 years, 1 month ago
Im currently running my terrain loading in a seperate thread. However, if the user chooses to exit the program while the thread runs, it will crash horribly, because resources, that the thread is trying to load, have been deleted behind it. Can I in any way kill a thread before I clean up resources that it may be using? I couldn't find anything in the documentation (since the thread doesn't die just because the thread-object dies). Thanks in advance.
Quote:CalvinI am only polite because I don't know enough foul languageQuote:Original post by superpigI think the reason your rating has dropped so much, Mercenarey, is that you come across as an arrogant asshole.
Advertisement
You have to notify the other thread somehow (via a message queue, synchronized global variable, etc.) that it should stop, then wait for it to exit with thread::join(). Boost.Threads doesn't offer a way to do a "hard" asynchronous kill precisely because it wouldn't be safe and would probably leak resources.
But for the thread to call join on itself, the class, that owns the operator()() must have knowledge of its owning thread, and then call join() on it.

I have another somewhat similar solution, where I collect all the threads in a threadcentral (singleton), and then the class that runs inside the thread adds it when it starts (in the beginning of operator()()) and removes it when it ends.

If at any point the program is exiting, the main thread can access the threadcentral and ask all threads to join().


However, it doesn't work. I get an assertion error.

Error message:
Assertion failed!
File: libs\thread\src\thread.cpp
Line: 222

Expression: res == WAIT_OBJECT_0



Is there really not a more elegant way to do this in the first place?
Quote:CalvinI am only polite because I don't know enough foul languageQuote:Original post by superpigI think the reason your rating has dropped so much, Mercenarey, is that you come across as an arrogant asshole.
Quote:Original post by Mercenarey
But for the thread to call join on itself, the class, that owns the operator()() must have knowledge of its owning thread, and then call join() on it.
No, the parent thread calls join(). The child thread just returns, at which point the join() call in the parent thread returns. Reread the documentation for join().
Yes, I understand the join(), and I tried to solve it by adding the active threads to a threadcentral, where the main thread can call join() on all threads, before it closes down.

I just don't understand why I get that assertion error :(


But isn't there a more elegant solution? What do you do in your code?
Quote:CalvinI am only polite because I don't know enough foul languageQuote:Original post by superpigI think the reason your rating has dropped so much, Mercenarey, is that you come across as an arrogant asshole.
Quote:Original post by Mercenarey
I just don't understand why I get that assertion error :(
No idea. Perhaps you should see what that assertion is checking?
Quote:
But isn't there a more elegant solution? What do you do in your code?

A more elegant solution than asking the thread to exit, and then waiting for it to do so? What strikes you as inelegant about that? That is The Way People Do It.
The Thread object alone isn't an indicator whether the thread is still running or not. So how do I know whether the Thread object is still valid?

If I have a list of Thread objects, any number of them might contain threads that finished. So making a list of all threads isn't really any good - I don't know when the thread belonging to the object stopped, so I can remove the object from my list.

My current solution is to let the thread add/remove itself from the list, but I don't find it particularly elegant, because it requires that the object going into the thread-object (the object containing operator()()) knows of the thread object owning it. Alot of circular stuff, that I find ugly personally.

And then the thread can't just delete itself from inside (that is what caused the assertion when I tried that), so even after I remove the object from the list, I still have to worry about how to delete it.

I don't find the thing I did particularly elegant, and that is why I asked whether you knew a more elegant way.

Couldn't you just throw some code? Show how you register the thread objects, when you call join() on shutdown, and how you clean them up when the thread ran out (and not only on shutdown).
It could save us more time seeing code.
Quote:CalvinI am only polite because I don't know enough foul languageQuote:Original post by superpigI think the reason your rating has dropped so much, Mercenarey, is that you come across as an arrogant asshole.

This topic is closed to new replies.

Advertisement