Ubuntu-specific pthread issue
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
gcc-defaults (Ubuntu) |
Invalid
|
Undecided
|
Unassigned |
Bug Description
The following c++11 program works properly (prints fn1 and fn2) on every system and compiler I have checked except for Ubuntu - where it prints (fn2 and fn2). This happens on both laptop and server installs of Ubuntu 18.04.
All system gcc packages on my Ubuntu 18.04 system show the same (incorrect) output:
g++-5 (Ubuntu 5.5.0-12ubuntu1) 5.5.0 20171010
g++-6 (Ubuntu 6.5.0-2ubuntu1~
g++-7 (Ubuntu 7.5.0-3ubuntu1~
g++-8 (Ubuntu 8.3.0-26ubuntu1
Notes:
- Re-compiling gcc from source eliminates the problem.
- I have not checked any Ubuntu versions other than 18.04.
Off-topic note:
- this may be a [libpthread bug](https:/
/* Minimal bug-reproducer for gcc on ubuntu */
#include <functional>
#include <thread>
#include <stdio.h>
struct run {
const std::function<
run(const std::function<
void operator()() {
fn();
}
};
int main() {
auto fn1 = [](){ printf("fn1\n"); };
auto r1 = run(fn1);
auto fn2 = [](){ printf("fn2\n"); };
auto r2 = run(fn2);
std::thread t1( r1 );
std::thread t2( r2 );
t1.join();
t2.join();
}
Update: this does not involve threads (see new minimal version below). Also, adding -O2 to the above version (with threading) gives no output, but creates a SEGV with the version below. Finally, the bug occurs at compile-time, not link time, since the object file (".o") created from this also errors when run on CentOS.
#include <functional>
#include <stdio.h>
struct run { void()> &fn; void()> &fn_) : fn(fn_) {}
const std::function<
run(const std::function<
void operator()() {
fn();
}
};
int main() {
auto fn1 = [](){ printf("fn1\n"); };
auto r1 = run(fn1);
auto fn2 = [](){ printf("fn2\n"); };
auto r2 = run(fn2);
r1();
r2();
}