Программы на C++. Форум разработчиков
-
Anonymous
Может ли поток, который позже получит спин-блокировку, произойти первым на временной шкале?
Сообщение
Anonymous »
Рассмотрим этот пример:
Код: Выделить всё
#include
#include
#include
struct SpinLock{
std::atomic state;
void lock(){
bool expected = false;
while(!state.compare_exchange_strong(expected,true,std::memory_order::acquire,std::memory_order::relaxed)){
expected = false;
}
}
void unlock(){
state.store(false,std::memory_order::release);
}
};
int main(){
auto spin_lock = SpinLock{false};
int i = 0;
std::thread t1([&](){
std::this_thread::sleep_for(std::chrono::seconds(1));
spin_lock.lock();
auto time_stamp = std::time(nullptr);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79098367/can-the-thread-that-acquires-spin-lock-later-happen-first-on-the-timeline[/url]
1729213993
Anonymous
Рассмотрим этот пример:
[code]#include
#include
#include
struct SpinLock{
std::atomic state;
void lock(){
bool expected = false;
while(!state.compare_exchange_strong(expected,true,std::memory_order::acquire,std::memory_order::relaxed)){
expected = false;
}
}
void unlock(){
state.store(false,std::memory_order::release);
}
};
int main(){
auto spin_lock = SpinLock{false};
int i = 0;
std::thread t1([&](){
std::this_thread::sleep_for(std::chrono::seconds(1));
spin_lock.lock();
auto time_stamp = std::time(nullptr);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79098367/can-the-thread-that-acquires-spin-lock-later-happen-first-on-the-timeline[/url]