LIU Hao wrote:

> 在 2026-10-7 15:54, Kirill Makurin 写道:
>> Martin Storsjö <[email protected]> wrote:
>>> Yes, I agree. I think that toolchains linking in winpthreads as a default
>>> library is becoming less common, so for them, requiring winpthreads for
>>> C11 threads sounds suboptimal.
>>
>> This also a good argument against implementing C11 threads around 
>> winpthreads.
>
> In GCC it's a default library for the posix thread model. It's like on other 
> platforms libpthread.a isn't
> linked unless `-pthread` is specified, but for *-w64-mingw32 with the posix 
> thread model it's the
> inverse: libpthread.a is linked by default unless `-no-pthread` is specified.
>
> I don't know the reason; maybe it's because winpthreads provides 
> `nanosleep()` so has to be a default
> library. And the reason why `nanosleep()` has to be in winpthreads is because 
> it's a cancellation point.

It might be the case. It's somewhat unrelated, but I saw some discussions on 
bug-gnulib about them using winpthreads just for `nanosleep`. Some time ago 
gnulib switched to using Win32 threads by default on native Windows, even with 
mingw-w64. The reason was some longstanding bugs which were not fixed.

>> One benefit of winpthreads is that me and Pali trying to make it work on old 
>> Windows versions, so users can produce binaries for those Windows versions. 
>> Regardless, I think that toolchains switching to better alternatives (e.g. 
>> gcc using native Windows TLS by default) is a good thing for majority of the 
>> users; those who need support for older systems can configure the toolchain 
>> appropriately.
>
> If you are looking for something for Windows 7+, you can also have a look at
> https://github.com/lhmouse/mcfgthread/blob/a411d0cb43e0f0c8a162c56c61fc0fcf35072db9/mcfgthread/c11.h
>
> I don't want to introduce incompatibility with Microsoft CRT or potential 
> mingw-w64 implementation, so
> all functions are inline.

I see, it's nice to know about one more alternative.

>>> As for the other points made earlier - I'm not sure that we need to have
>>> them ABI compatible with MSVC's version - probably not. If we can it would
>>> be nice though.
>>
>> Yes, this would be nice. One potential issue is MSVC changing its ABI at 
>> some point in future. However, given that there were only a few ABI changes 
>> in public CRT functions, I think chances of this are pretty low.
>
> Despite C11 threading being introduced in Visual Studio 2022 version 17.8.0, 
> the DLL name
> `vcruntime140_threads.dll` still suggests Visual Studio 2015 where UCRT was 
> introduced, which seems a
> subtle hint about ABI compatibility.

Yes.

>>> Likewise, I think don't think that it's strictly necessary to have the
>>> code in a separate DLL at all. Users diskike extra runtime DLLs.
>>
>> This a good point. I wish we could give users a choice, with static being 
>> the default. I think it would be really nice if we could handle it in a way 
>> similar to -static-libgcc and -shared-libgcc options.
>>
>> Would it be possible for use to first add such a library, and then introduce 
>> a compiler option for using it?
>
> In GCC those are handled in the builtin specs file; see `gcc -dumpspecs`.
>
> But this idea goes against the common sense that libraries are shared by 
> default. Probably not good.

IIRC, on Linux, default gcc behavior is to link static libgcc for C and shared 
libgcc for C++. If I recall correctly, it is required for C++ exceptions to 
function correctly. So, I guess some exceptions to this rule would be ok?

>> One such case would be functions like `pthread_self`, or in case of C11 
>> threads, it would be `thrd_current`. For example, if EXE creates a thread 
>> and then DLL calls `thrd_current` from that thread, there will be two 
>> `thrd_t` object for the same thread.
>>
>> Such cases are an example why I think using winpthreads as a DLL should 
>> always be preferred.
>
> The Microsoft `thrd_t` is a structure of two fields: the handle is for 
> `thrd_join()`, and the numeric
> identifier is for `thrd_equal()`. But there still be issue if 
> `thrd_detach(thrd_current())` is called.

Maybe following MSVC ABI is not the best idea after all...

> In mcfgthread, the TLS index for `TlsGetValue()` is stored in named shared 
> memory, so all instances of
> the library in the same process get the same value, no matter whether they 
> are linked statically or
> dynamically.

This sounds like a good solution. I think we should do the same in winpthreads 
eventually.

- Kirill Makurin

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to