Last active
October 5, 2016 10:36
-
-
Save rettichschnidi/1c68ae6f40f14960f803 to your computer and use it in GitHub Desktop.
gcc TSAN fail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
Author
rettichschnidi
commented
Jan 20, 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