I am working on a game, so at one point I had to use fork(), the main thread runs opengl graphics, and the child thread runs the game logic.
Now I have a problem. At some point, the user may press the ‘Exit’ button inside the game, which is handled by the secondary thread. Also, at some point the user may click the X button to exit the game which is handled by the main (glut) thread. So here is my question: how can I kill the other running thread, and exit?
Right now, if I close the window, the second thread keeps running, and if the second thread finishes, the first one keeps running.
Handling the ‘X’ button could be done using the atexit function, but I haven’t found a (safe) way of killing glutMainLoop().
If you are actually calling
fork()(instead of starting a new thread) then you are actually creating another running process. Whenfork()is called, it returns a process-id to the parent process. That can be passed tokill()to kill the process. Under linux this looks like:You need to choose what signal to send the process. SIGKILL (9) kills it hard for example.