yohhoyの日記

技術的メモをしていきたい日記

main関数returnと他スレッドの関係

プログラミング言語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 with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main 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 calling std::exit with the return value as the argument. (snip)

関連URL

*1:規格上の表現では "control is returned to the host environment"