On Wednesday 07 October 2026 12:37:09 Kirill Makurin wrote: > Pali Rohár <[email protected]> wrote: > > > On Wednesday 07 October 2026 12:15:00 Kirill Makurin wrote: > >> The issue here that any existing instance of `thrd_t` for the thread will > >> not be updated, and will contain old handle value. > > > > I think that this is not a problem because: > > - every existing instance contains the required information about thread > > (which is thread id) and this is always correct > > - for comparing two instances is needed to use thrd_equal() function > > which even in the vcruntime140_threads.dll is using the thread id (and > > not thread handle) > > - if needed, thread id can be converted to handle by opening a thread by > > id (there is a winapi function for it) > > > > Maybe it is not easy to use this structure, but it contains all data for > > thread identification and usage. > > A thread that was detached with `thrd_detach` cannot be joined with > `thrd_join` or vice versa; doing so is undefined behavior. Well, in case of > MSVC implementation, it seems like it will actually work.
That is only for _successfully_ detached. Because of NULL handles returned by thrd_current(), the thrd_detach call fails and returns error. So if the thrd_detach(thrd_current()) fails then the parent thread could still successfully call thrd_join(child_thrd) and here the child_thrd contains the valid non-NULL handle (so it will success). Idea for thrd_detach(thrd_current()) implementation: - thrd_create() will additionally store the handle of child thread to the TLS storage of the child thread (ideally using the shared memory trick, so even static linking of same thrd_create function multiple times would work) - thrd_detach() will check if the thread HANDLE (from passed pair) is NULL and if yes, it will retrieve the real HANDLE from TLS storage of the thread identified by threadid (from passed pair) _______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
