<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63841>63841</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            std::thread creation spuriously fails
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dvyukov
      </td>
    </tr>
</table>

<pre>
    While running a program I got a spurious std::system_error exception.
The problem is that std::thread does very simple error handling for pthread_create:
https://github.com/llvm/llvm-project/blob/96ae0851c26237378fa1280b0a9ad713e1b72bdb/libcxx/include/__thread/thread.h#L257-L258
https://github.com/llvm/llvm-project/blob/96ae0851c26237378fa1280b0a9ad713e1b72bdb/libcxx/include/__threading_support#L374

But on Linux clone system call can spuriously fail with EAGAIN:
https://github.com/torvalds/linux/commit/498052bba55ecaff58db6a1436b0e25bfd75a7ff

I think std::thread should do what Go runtime does to avoid spuroius exceptions:
https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/runtime/cgo/gcc_libinit.c#L99-L111
Unfortunately it's not a good idea to loop infinitely since EAGAIN can be returned for other reasons (no resources, task limit reached).

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMlE-PpDYTxj-N-2JNC8pNAwcOPbsvq5FG7ylRjq2yXYAzxkb-0zv97SPTk0yUvWxukRAYqVz6lZ_nMcZoZkc0sOaZNV8PmNPiw6Bv9_zmbwfp9X34bTGWeMjOGTdz5Fvwc8CVv_DZJ448bjkYnyOPSTNxYeIS7zHReqUQfOD0rmhLxrsjq76y6vLLQqWFtLRyE3laMH3uTEsg1Fx7ivxG4c6jWTdL_NFqQadtgZh84Nuj9qoCYaKyfW-_pLTF8gcjg3E2acnyqPzKYLT29ufnaQv-d1KJwSitlwzG_oxUdU2t4AyiFW03YQ1dJSvsUbe1oFq2IHUptUaq93cGo3HKZk0Mxuv1QcNgfCyOCwPxCk379ApN919AM26-xrxtPqSCJtrTg-rxfs6Je8dfjcvvXFnviD9U5Aqt5QrdXzrbO5_QWP7dpIX_7_Lt8vL_nzn95MMNrY47pcsFUvl1NWXQU99VDUiJTUMKp6nptDxjfRJnWRE0ctJtg-00_Z34hafFuLcfvBMXn22xEP9erPXNF-sms9LDVclzvHmj93G8yfHToPFnxpi9RTfvi0-FgCS1LcKksSbqp-k86R4FVV0vBOqaJBJWomcwxqAYjB9I5Qz2PrNSV2ukcSYdVZGn759e67p-4PzqJh9SdpjI3nk5sTZyt4dv9l5zownLYNb7jRs3lTalMhqn6EOiXUJJPFDKwZHeM-TTQoEHwuhd5Aw653mg6HNQFBl84QnjG7dmNalUqYU0g_4jyAc9CN2LHg801OeuB1FD2x6WQU-TQJBdBdj0YsKpbUjorgJQulfT6WAGqEBUbS2qc3MS7bE_n1olmxPIHgShYKeKVjT2WAJx9GE-mBgzDWfRneqDRUk27lcWgMwzA2DwhQEU9zN43h8o11kY9kTJPEd2qqyJKX62TCZZGv5pn_06Mf4Hu8dDDnb41xHeuYvnd_Q_AgAA___aa8Lk">