#include <iostream>
#include <atomic>
#include <thread>
#include <list>
using namespace std;
atomic<bool> is_wrong(false);
void test () {
for (size_t i = 0; i < 100; ++i) {
if (rand() % 1000 == 0) { // uproszczona sytuacja
is_wrong = true; // o TU
return;
}
}
}
int main () {
size_t number_of_threads = 10;
list <thread> threads;
for (size_t i = 0; i < number_of_threads; ++i)
threads.emplace_back(test);
for (thread &t : threads)
t.join();
cout << is_wrong;
return 0;
}
Dzień dobry. Chcę, żeby wszystkie "wątki dzieci" zostały przerwane w momencie ustawienia is_wrong
na true
w którymś z nich. Jasne - mógłbym zrobić
void test () {
for (size_t i = 0; i < 100 && !is_wrong; ++i) {
if (rand() % 1000 == 0) { // uproszczona sytuacja
is_wrong = true; // o TU
return;
}
}
}
ale chodzi o rozwiązanie asynchroniczne, tj bez ciągłego sprawdzania stanu is_wrong
.
satirev