C++ 中的测试和测试并设置

Maj*_*jnu 5 c++ multithreading synchronization locking

我正在尝试用 C++ 实现测试和测试并设置。

\n\n

根据C++atomic_flag 查询状态\n我们无法检查标志状态,那么解决方法是什么

\n\n

循环会是这样的

\n\n
std::atomic_flag lock_stream = ATOMIC_FLAG_INIT;\nvoid ttas_lock()\n{\n    while(lock_Stream);\n    while(lock_stream.test_and_set());\n    return\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误:无法将 \xe2\x80\x98((ttas*)this)->ttas::lock_stream\xe2\x80\x99 从 \xe2\x80\x98std::atomic_flag\xe2\x80\x99 转换为 \xe2\ x80\x98bool\xe2\x80\x99\n while(lock_stream)

\n

Pau*_*ans 0

你的职能:

void ttas_lock()
{
    while(lock_Stream);
    if(!lock_stream.test_and_set()) return;
}
Run Code Online (Sandbox Code Playgroud)

永远return之后。lock_Streamfalse不是你想要的。