Hello everyone, I am using multi threading in c++11. I want to make mechanism in which it will keep track of all running and terminated threads.If any thread is terminated then it will automatically create the terminated thread. Anyone have idea regarding how to implement this in c++11? Thank you very much in advance.
Asked
Active
Viewed 106 times
0
-
1I assume that you will *join* the terminated thread? A simple solution is just an atomic boolean flag that the thread sets last thing it does before exiting. The main thread polls this flag and if set then join the thread. – Some programmer dude Feb 06 '17 at 13:00
-
To add on @Someprogrammerdude comment, note that some *thread libraries* (e.g. *pthreads*) provide an additional *non-blocking join* which removes the problem of *polling* the *thread state* before *joining* it. It might be less efficient but it should result in a cleaner design. – Patrick Trentin Feb 06 '17 at 13:09
-
Thanks for your replay @Someprogrammerdude. i have never used an atomic flag before. where should i have to add this boolean flag in my program? – parth gandhecha Feb 06 '17 at 14:25
-
What do you mean under "If any thread is terminated then it will automatically create the terminated thread."? Do you want to implement thread pool? in this case you don't need to terminate any threads. – Alexey Guseynov Feb 06 '17 at 17:00
-
@AlexeyGuseynov if accidentally any thread got terminated then it will crate terminated thread. for example if one of my thread is calculating divison of two numbers based on user input and if user enters divisor as 0 then thread got terminated. i want to make mechanism in which this terminated thread will be recreated. – parth gandhecha Feb 10 '17 at 05:48
-
@PatrickTrentin can you let me know some of the thread libraries for c++11? – parth gandhecha Feb 10 '17 at 05:49
-
*insert-web-search-engine-name-of-our-liking* is your friend: https://en.wikipedia.org/wiki/List_of_C%2B%2B_multi-threading_libraries – Patrick Trentin Feb 10 '17 at 06:13
-
Modern C++ provides thread support library in STL. There is no reason to use any other library in new projects unless you have really unusual requirements. – Alexey Guseynov Feb 10 '17 at 07:21
-
@AlexeyGuseynov sure, I just gave him what he asked – Patrick Trentin Feb 10 '17 at 11:44
1 Answers
0
Common pattern to write code, that survives exceptions is to wrap it in a loop and try block, like this:
for(;;) {
try {
// Your code here
} catch (const std::exception& e) {
// Log exception
} catch (...) {
// Log unknown exception
}
}
It is common in servers: if serving user request fails, then log it, drop current connection and try to serve next one. Of course there is a chance that something really bad happened and all further retries will fail. So you must be careful not to overload your PC with infinite loop which tries again and again.
You can put this loop inside your thread so it will not crash if something happens, or, instead of using threads directly you can use std::future
and put future.get() code inside this loop.

Alexey Guseynov
- 5,116
- 1
- 19
- 29