std:: atomic_fetch_and, std:: atomic_fetch_and_explicit
|
헤더에 정의됨
<atomic>
|
||
|
template
<
class
T
>
T atomic_fetch_and
(
std::
atomic
<
T
>
*
obj,
|
(1) | (C++11부터) |
|
template
<
class
T
>
T atomic_fetch_and
(
volatile
std::
atomic
<
T
>
*
obj,
|
(2) | (C++11부터) |
|
template
<
class
T
>
T atomic_fetch_and_explicit
(
std::
atomic
<
T
>
*
obj,
|
(3) | (C++11부터) |
|
template
<
class
T
>
T atomic_fetch_and_explicit
(
volatile
std::
atomic
<
T
>
*
obj,
|
(4) | (C++11부터) |
obj 가 가리키는 값을 원자적으로 obj 의 이전 값과 arg 의 비트 AND 연산 결과로 대체합니다. obj 가 이전에 보유하고 있던 값을 반환합니다.
이 작업은 다음과 같이 실행되는 것처럼 수행됩니다:
만약
std::atomic<T>
에
fetch_and
멤버가 없는 경우(이 멤버는
integral types
중
bool
을 제외한 타입에만 제공됨), 프로그램은 ill-formed입니다.
목차 |
매개변수
| obj | - | 수정할 원자적 객체에 대한 포인터 |
| arg | - | 원자적 객체에 저장된 값에 비트 단위 AND 연산을 수행할 값 |
| order | - | 메모리 동기화 순서 |
반환값
이 함수의 효과가 적용되기 직전의 값으로, modification order 에서 * obj 의 값입니다.
예제
#include <atomic> #include <chrono> #include <functional> #include <iostream> #include <thread> // Binary semaphore for demonstrative purposes only. // This is a simple yet meaningful example: atomic operations // are unnecessary without threads. class Semaphore { std::atomic_char m_signaled; public: Semaphore(bool initial = false) { m_signaled = initial; } // Block until semaphore is signaled void take() { while (!std::atomic_fetch_and(&m_signaled, false)) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } void put() { std::atomic_fetch_or(&m_signaled, true); } }; class ThreadedCounter { static const int N = 100; static const int REPORT_INTERVAL = 10; int m_count; bool m_done; Semaphore m_count_sem; Semaphore m_print_sem; void count_up() { for (m_count = 1; m_count <= N; ++m_count) if (m_count % REPORT_INTERVAL == 0) { if (m_count == N) m_done = true; m_print_sem.put(); // signal printing to occur m_count_sem.take(); // wait until printing is complete proceeding } std::cout << "count_up() done\n"; m_done = true; m_print_sem.put(); } void print_count() { do { m_print_sem.take(); std::cout << m_count << '\n'; m_count_sem.put(); } while (!m_done); std::cout << "print_count() done\n"; } public: ThreadedCounter() : m_done(false) {} void run() { auto print_thread = std::thread(&ThreadedCounter::print_count, this); auto count_thread = std::thread(&ThreadedCounter::count_up, this); print_thread.join(); count_thread.join(); } }; int main() { ThreadedCounter m_counter; m_counter.run(); }
출력:
10 20 30 40 50 60 70 80 90 100 print_count() done count_up() done
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| P0558R1 | C++11 |
정확한 타입 일치가 요구되었음
T
가 여러 인수에서 추론되었기 때문
|
T
는 오직 obj로부터만
추론됨 |
참고 항목
|
인자와 원자 객체의 값 사이에 비트 AND를 원자적으로 수행하고 이전에 보유한 값을 획득합니다
(
std::atomic<T>
의
public member function)
|
|
|
(C++11)
(C++11)
|
원자 객체를 비원자 인자와의 비트 OR 결과로 대체하고 원자 객체의 이전 값을 획득합니다
(function template) |
|
(C++11)
(C++11)
|
원자 객체를 비원자 인자와의 비트 XOR 결과로 대체하고 원자 객체의 이전 값을 획득합니다
(function template) |
|
C documentation
for
atomic_fetch_and
,
atomic_fetch_and_explicit
|
|