g++ -Wl,--as-needed -pthread behaviour

Jonathan Wakely jwakely.gcc@gmail.com
Tue Sep 24 12:48:00 GMT 2013

On 24 September 2013 13:45, Jakub Jelinek wrote:
> On Tue, Sep 24, 2013 at 01:34:52PM +0100, Jonathan Wakely wrote:
>> On 24 September 2013 13:24, Jakub Jelinek wrote:
>> > On Tue, Sep 24, 2013 at 01:13:53PM +0100, Jonathan Wakely wrote:
>> >> It's the std::thread constructor template that needs pthread_create.
>> >> std::thread::join() needs pthread_join.
>> >
>> > Are any references to that needed in libstdc++.so.6, or just in headers?
>> It's called from libstdc++-v3/src/c++11/thread.cc which ends up in
>> libstdc++.so.6
> Ah, then it has to use __gthread_create.
>> > Having libstdc++.so.6 depend on libpthread.so is not a good idea, the
>> > latter might be possible by just referencing pthread_* instead of
>> > __gthread_* where you actually require it.
>> For targets that don't use gthr-posix.h __gthread_create is not a
>> wrapper for pthread_create.
> Grep tells me that other gthr* headers don't define __gthread_create at all.

Yet :-)

I have tried to implement the full C++11 thread facilities on Windows,
but gave up because mingw was such a PITA to build, let alone test.  I
decided I'd rather smash my teeth out with a hammer than continue
wasting time on such an annoying target, but someone more patient
might want to make it work one day.

> Anyway, either we just declare that people who use --as-needed without
> thinking about the consequences get what they deserve, or you'd need to
> keep the current std::thread::_M_start_thread as is in libstdc++.so.6, but
> in newer headers inline it, perhaps under a different name (using pthread_create
> rather than the wrapper).

Yes, that would be an option.

> I guess for other routines it isn't really needed, thread::join if
> nobody started a thread doesn't look like a good idea, similarly detach.


More information about the Gcc mailing list