[Bug 273398] lang/gcc: statically linking a program with -pthread aborts

From: <bugzilla-noreply_at_freebsd.org>
Date: Mon, 28 Aug 2023 11:43:20 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=273398

            Bug ID: 273398
           Summary: lang/gcc: statically linking a program with -pthread
                    aborts
           Product: Ports & Packages
           Version: Latest
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: Individual Port(s)
          Assignee: gerald@FreeBSD.org
          Reporter: stffn.mobil@freenet.de
             Flags: maintainer-feedback?(gerald@FreeBSD.org)
          Assignee: gerald@FreeBSD.org

I have created a simple program using two C++ threads:
```
#include <iostream>
#include <mutex>
#include <thread>

int num = 0;
std::mutex mut{};

void func() {
        for (int i = 0; i < 10000; ++i) {
                std::lock_guard lock(mut)
                ++num;
        }
}

int main() {
        std::thread thread1 = std::thread(func);
        std::thread thread2 = std::thread(func);

        thread2.join();
        thread1.join();

        printf("%d\n", num);

        return 0;
}
```

Compiling this with `g++13 -static -O3 -pthread main.cpp` and running `./a.out`
generates a core dump: 
```
zsh: abort (core dumped)  ./a.out
```

I've tried linking with `-lthr` and `-static-libstdc++` and `-static-libgcc`
but those should be implied by `g++ -static` anyways.

I'm working on a project where this issue occures aswell when using the
pthread.h header instead of C++ threads.

Clang's static and dynammic output works as expected as well as gcc's dynamic
executable.

-- 
You are receiving this mail because:
You are the assignee for the bug.