std::atomic counter = 0; // memory_order_seq_cst
int globalInt1;
int globalInt2;
int globalInt3;
void thread1()
{
// ++counter;
globalInt1 = 1;
--counter;
Wait();
}
void thread2()
{
//++counter;
globalInt2 = 2;
--counter;
Wait();
}
void thread3()
{
// ++counter;
globalInt3 = 3;
--counter;
Wait();
}
int main()
{
++counter;
++counter;
++counter;
std::thread{ thread1 }.detach();
std::thread{ thread2 }.detach();
std::thread{ thread3 }.detach();
while (counter != 0);
// Can I get the value of globalInt1~3 correctly here?
}
< /code>
Привет всем. Мой вопрос заключается в том, могу ли я правильно получить GlobalInt1 ~ 3 в основном потоке, или основной поток может синхронизировать только с определенным потоком, который в последний раз изменял переменную ?
Подробнее здесь: https://stackoverflow.com/questions/797 ... ic-variant
Межпоточная синхронизация с одним атомным вариантом ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение