-1

Go calls the C function, in which a large number of threads will be created and bound to the specified CPU core;

When a CPU is 100% occupied by other applications, the main program will call os.Exit(0) to exit and wait for a long time before exiting

golang version:1.12.5

Os. Exit() can immediately exit the program

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441

1 Answers1

-1

Exit will call panic if Exit code is 0 and panic on exit (testlog. SetPanicOnExit0) is set. Otherwise it calls syscall.Exit. Child process are send a SIGCHLD signal it will wait for child process to exit.

whitespace
  • 789
  • 6
  • 13
  • oth os.Exit (0) and os.Exit (1) have been tried. The result is that it takes a long time to exit, but before I call os.Exit, call pthread first_ cancel; pthread_ Join ends the thread, then it can immediately exit – yangxingxiang Feb 13 '23 at 10:06
  • Does os.exit send a signal to the child process to notify the end? If not, how does the child thread know that it needs to end? Or does the parent thread os.exit exit first, and the child thread exits automatically. But from the problem I encountered, that's not the case – yangxingxiang Feb 13 '23 at 10:14
  • C thread exits when the main function exits, it is as per behaviour of C. [c thread](https://stackoverflow.com/questions/11875956/when-the-main-thread-exits-do-other-threads-also-exit#:~:text=All%20the%20threads%20in%20your,you%20return%20from%20main()%20.) – whitespace Feb 14 '23 at 03:30
  • But the problem I have now is that the OS.exit has been blocked for a long time before the C thread exits. Only the active pthread_ Cancel allows the C thread to exit, and os.exit exits in time – yangxingxiang Feb 15 '23 at 04:50