https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115421
Bug ID: 115421
Summary: Multi-threaded condition_variable app throws when
linking as -static on Linux
Product: gcc
Version: 14.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: ilg at livius dot net
Target Milestone: ---
Created attachment 58398
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58398&action=edit
The test source code
I have a test that checks C++ threads which synchronise via a
condition_variable.
The test runs fine on Linux/macOS/Windows, in various scenarios, except when I
build it as `-static` on Linux.
```
$ g++ sleepy-threads-cv.cpp -o sleepy-threads-cv -static -g -lpthread
$ ./static-sleepy-threads-cv 4
abcd
$ g++ sleepy-threads-cv.cpp -o static-sleepy-threads-cv -static -g -lpthread
$ ./static-sleepy-threads-cv 4
Segmentation fault
```
On some systems the result is an exception:
```
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
```
I encountered the issue while building the xPack GCC 14 binaries, but later
confirmed it on GCC 13 and older.
The source code is attached.inside
The problem seems to be the global condition variable and/or mutex; if I move
them inside the `spawnThreads()` function and pass pointers to them to all
threads, the test is functional even with `-static`.
Is there anything wrong with my test, or is this a small bug in libstdc++?