View Issue Details

IDProjectCategoryView StatusLast Update
0006403Rocky-Linux-8gcc-toolset-12public2024-04-27 07:15
ReporterChristopher Cooley Assigned To 
PrioritynormalSeveritycrashReproducibilityhave not tried
Status newResolutionopen 
OSRocky LinuxOS Version8.6 
Summary0006403: Crash in _M_start_thread when using C++ 11 threads in a dlopened library
DescriptionMinimal example:

I have two source files executable.cpp and library.cpp, which are compiled into an executable and dynamic library, respectively. The executable dlopens the dynamic library, and calls a function within it. The dynamic library function starts a thread.

This all works fine when the executable is compiled WITHOUT -lstdc++. However, when the executable is compiled WITH -lstdc++, a segmentation fault occurs. Compiling with -lstdc++ only seems to work if -lpthread is also used to compile the executable, even though the executable itself does not uses threads. This was unexpected behavior to me.



#include <dlfcn.h>
#include <stddef.h>

int main(int argc, char **argv)
        void *libraryObject;

        void (*libraryFunction)(void);

        libraryObject = dlopen("./", RTLD_NOW);
        if(libraryObject == NULL)
                return 1;

        libraryFunction = (void (*)(void)) dlsym(libraryObject, "libraryFunction");
        if(libraryFunction == NULL)
                return 1;


        return 0;


#include <thread>
#include <iostream>

extern "C" void libraryFunction(void);

void threadFunction(void);

void libraryFunction(void)
        std::thread threadObject(threadFunction);

void threadFunction(void)
        std::cout << "Hello from threadFunction" << std::endl;
Steps To ReproduceCompiling the executable without -lstdc++ works:

$ cc library.cpp -o -shared -fpic -lpthread -lstdc++
$ cc executable.cpp -o executable -ldl
$ ./executable
Hello from threadFunction

Compiling the executable with -lstdc++ and without -lpthread does not work:

$ cc library.cpp -o -shared -fpic -lpthread -lstdc++
$ cc executable.cpp -o executable -ldl -lstdc++
$ ./executable
Segmentation fault (core dumped)

Compiling the executable with -lstdc++ and -lpthread does work:

$ cc library.cpp -o -shared -fpic -lpthread -lstdc++
$ cc executable.cpp -o executable -ldl -lstdc++ -lpthread
$ ./executable
Hello from threadFunction

Backtrace of crash:

(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff78f8e89 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) ()
   from /lib64/
#2 0x00007ffff7ff0678 in std::thread::thread<void (&)(), , void>(void (&)()) () from ./
#3 0x00007ffff7ff0498 in libraryFunction () from ./
#4 0x000000000040118f in main ()
Additional InformationOutput of cc -v:

Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/gcc-toolset-12/root/usr --mandir=/opt/rh/gcc-toolset-12/root/usr/share/man --infodir=/opt/rh/gcc-toolset-12/root/usr/share/info --with-bugurl= --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.2.1-20221121/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.1 20221121 (Red Hat 12.2.1-7) (GCC)
TagsNo tags attached.


There are no notes attached to this issue.

Issue History

Date Modified Username Field Change
2024-04-27 07:15 Christopher Cooley New Issue