желаемый результат может быть достигнут с двумя атомными флагами, такими как в демонстрации:
Код: Выделить всё
#include
#include
#include
std::atomic_flag ping_lock_flag = ATOMIC_FLAG_INIT;
std::atomic_flag pong_lock_flag = ATOMIC_FLAG_INIT;
std::atomic_int idx = 0;
std::vector v(10);
void update()
{
ping_lock_flag.wait(false, std::memory_order_relaxed);
ping_lock_flag.clear(std::memory_order_release);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79478387/two-threads-alternating-ping-pong-execution[/url]
Мобильная версия