[Bug c++/92731] Data race on exception object thrown from std::future
rrrlasse at hotmail dot com
gcc-bugzilla@gcc.gnu.org
Sat Nov 30 06:46:00 GMT 2019
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92731
--- Comment #4 from Lasse Reinhold <rrrlasse at hotmail dot com> ---
Some simplifications, and also tried with atomics instead of mutexes, to no
avail:
#include <iostream>
#include <stdexcept>
#include <future>
#include <assert.h>
std::function<void()> job;
std::atomic<bool> has_job{ false };
int main() {
std::thread t = std::thread([]() {
for (;;) {
if (has_job.load(std::memory_order_acquire)) {
job();
has_job.store(false, std::memory_order_release);
}
}
});
for (int c = 0; ; c++) {
if (!has_job.load(std::memory_order_acquire)) {
try {
std::packaged_task<void()> packaged_task([]() { throw 1234; });
std::future<void> task_future = packaged_task.get_future();
job = [&]() {
std::packaged_task<void ()> tmp(std::move(packaged_task));
tmp();
};
has_job.store(true, std::memory_order_release);
task_future.get();
}
catch (const int& e) {
assert(e == 1234);
}
}
}
}
More information about the Gcc-bugs
mailing list