Skip to content

Instantly share code, notes, and snippets.

@rettichschnidi
Last active October 5, 2016 10:36
Show Gist options
  • Save rettichschnidi/1c68ae6f40f14960f803 to your computer and use it in GitHub Desktop.
Save rettichschnidi/1c68ae6f40f14960f803 to your computer and use it in GitHub Desktop.
gcc TSAN fail
// Source: http://en.cppreference.com/w/cpp/thread/promise
#include <future>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>
void accumulate(std::vector<int>::iterator first,
std::vector<int>::iterator last,
std::promise<int> accumulate_promise) {
int sum = std::accumulate(first, last, 0);
accumulate_promise.set_value(sum); // Notify future
}
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
std::promise<int> accumulate_promise;
std::future<int> accumulate_future = accumulate_promise.get_future();
std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
std::move(accumulate_promise));
accumulate_future.wait(); // wait for result
std::cout << "result=" << accumulate_future.get() << '\n';
work_thread.join(); // wait for thread completion
}
@inetic
Copy link

inetic commented Oct 5, 2016

Hello Reto, I'm seeing this same message from g++'s thread sanitizer. Have you been able to confirm that this is a false positive? Or if it's not, a workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment