Рассмотрим пример ниже. Предположим, что
Код: Выделить всё
barrierThere is one producer thread and two consumer threads that constantly check
Код: Выделить всё
barrierКод: Выделить всё
runcntКод: Выделить всё
runcntIf the order is like it is written, I think the code would run as expected. But if the
Код: Выделить всё
barrierКод: Выделить всё
runcntAm I missing anything? Is there a way to fix this?
Код: Выделить всё
extern atomic barrier[2];
atomic_int runcnt{0};
void producer() {
runcnt.store(2, memory_order_relaxed);
barrier[0].store(1, memory_order_relaxed);
barrier[1].store(1, memory_order_relaxed);
while (runcnt.load(memory_order_relaxed)) {
cpu_pause();
}
}
void consumer(unsigned index) {
while (true) {
if (barrier[index].exchange(false, memory_order_relaxed)) {
int prev = runcnt.fetch_sub(1, memory_order_relaxed);
assert(prev > 0);
}
}
}
Источник: https://stackoverflow.com/questions/781 ... ple-stores
Мобильная версия