std:: atomic_flag
From cppreference.net
C++
Concurrency support library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::atomic_flag
| Member functions | ||||
|
(C++20)
|
||||
|
(C++20)
|
||||
|
(C++20)
|
||||
|
(C++20)
|
|
헤더 파일에 정의됨
<atomic>
|
||
|
class
atomic_flag
;
|
(C++11부터) | |
std::atomic_flag
는 원자적 불린 타입입니다.
std::atomic
의 모든 특수화와 달리, 이는 lock-free가 보장됩니다.
std::
atomic
<
bool
>
와 달리,
std::atomic_flag
는 load나 store 연산을 제공하지 않습니다.
멤버 함수
|
atomic_flag를 생성함
(public member function) |
|
|
[deleted]
|
대입 연산자 (삭제됨)
(public member function) |
|
플래그를 원자적으로
false
로 설정함
(public member function) |
|
|
플래그를 원자적으로
true
로 설정하고 이전 값을 얻음
(public member function) |
|
|
(C++20)
|
플래그의 값을 원자적으로 반환함
(public member function) |
|
(C++20)
|
알림을 받고 원자 값이 변경될 때까지 스레드를 차단함
(public member function) |
|
(C++20)
|
원자 객체를 기다리는 스레드 중 적어도 하나에게 알림을 보냄
(public member function) |
|
(C++20)
|
원자 객체를 기다리며 차단된 모든 스레드에게 알림을 보냄
(public member function) |
예제
스핀락 뮤텍스 데모는 사용자 공간에서 atomic_flag 를 사용하여 구현할 수 있습니다. 실제로 스핀락 뮤텍스는 매우 의심스러운 방식임을 유의하십시오.
이 코드 실행
#include <atomic> #include <iostream> #include <mutex> #include <thread> #include <vector> class mutex { std::atomic_flag m_{}; public: void lock() noexcept { while (m_.test_and_set(std::memory_order_acquire)) #if defined(__cpp_lib_atomic_wait) && __cpp_lib_atomic_wait >= 201907L // Since C++20, locks can be acquired only after notification in the unlock, // avoiding any unnecessary spinning. // Note that even though wait guarantees it returns only after the value has // changed, the lock is acquired after the next condition check. m_.wait(true, std::memory_order_relaxed) #endif ; } bool try_lock() noexcept { return !m_.test_and_set(std::memory_order_acquire); } void unlock() noexcept { m_.clear(std::memory_order_release); #if defined(__cpp_lib_atomic_wait) && __cpp_lib_atomic_wait >= 201907L m_.notify_one(); #endif } }; static mutex m; static int out{}; void f(std::size_t n) { for (std::size_t cnt{}; cnt < 40; ++cnt) { std::lock_guard lock{m}; std::cout << n << ((++out % 40) == 0 ? '\n' : ' '); } } int main() { std::vector<std::thread> v; for (std::size_t n{}; n < 10; ++n) v.emplace_back(f, n); for (auto &t : v) t.join(); }
가능한 출력:
0 1 1 2 0 1 3 2 3 2 0 1 2 3 2 3 0 1 3 2 0 1 2 3 2 3 0 3 2 3 2 3 2 3 1 2 3 0 1 3 2 3 2 0 1 2 3 0 1 2 3 2 0 1 2 3 0 1 2 3 2 3 2 3 2 0 1 2 3 2 3 0 1 3 2 3 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 3 2 0 2 3 2 3 2 3 2 3 2 3 0 3 2 3 0 3 0 3 2 3 0 3 2 3 2 3 0 2 3 0 3 2 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
참고 항목
|
(C++11)
(C++11)
|
플래그를 원자적으로
true
로 설정하고 이전 값을 반환합니다
(함수) |
|
(C++11)
(C++11)
|
플래그 값을 원자적으로
false
로 설정합니다
(함수) |
|
(C++20)
(C++20)
|
알림을 받고 플래그가 변경될 때까지 스레드를 차단합니다
(함수) |
|
(C++20)
|
atomic_flag_wait에서 차단된 스레드 하나에 알림을 보냅니다
(함수) |
|
(C++20)
|
atomic_flag_wait에서 차단된 모든 스레드에 알림을 보냅니다
(함수) |
|
(C++11)
|
std::atomic_flag
를
false
로 초기화합니다
(매크로 상수) |
|
C documentation
for
atomic_flag
|
|