Fix multiple issues when exiting

- pid should be set to 0 so we don't kill a thread that doesn't exist
  yet
- atexit is global to the program, not only main !