multithreading - How to restart a multithreaded C++ program inside the code? -
as describe in header have in thread if statement checked every 1 minute , if true restart whole programm.. suggestions?
void* checkthread(void* arg) { if(statement) //restart procedure sleep(60); } int main() { pthread_create(&thread1, null, checkthread, main_object); pthread_create(); pthread_create(); }
if going nuke-it-from-orbit approach (i.e. don't want trust code controlled shutdown reliably), having kill-and-auto-relaunch mechanism inside same process space other code not robust approach. example, if 1 of other threads crash, take auto-restart-thread down it.
a more fail-safe approach have auto-restart-thread launch of other code in sub-process (via fork(); calling exec() allowable not necessary in case). after 60 seconds, parent process can kill child process created (by calling kill() on process id fork() returned) , launch new one.
the advantage of doing way separating of memory spaces protects relauncher-code bugs in rest of code, , killing of child process means os handle cleanup of memory , other resources you, there less of worry things memory or file-handle leaks.
Comments
Post a Comment