プログラミング言語C/C++言語におけるmain関数returnと他スレッドの関係についてメモ。
C11/C++11ともに “メインスレッドのmain関数からのreturn” は標準ライブラリexit
関数呼び出しと等価であり、他に実行中スレッドが存在したとしても該当プロセスを終了する*1。
C11
JTC1/SC22/WG14 N1570 5.1.2.2.3/p1より一部引用。
If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument; (snip)
C++11
JTC1/SC22/WG21 N3337 3.6.1/p5より一部引用。
A return statement in
main
has the effect of leaving the main function (destroying any objects with automatic storage duration) and callingstd::exit
with the return value as the argument. (snip)
関連URL
*1:規格上の表現では "control is returned to the host environment"