std::atomic_fetch_or_explicit
STD::原子[医]去取[医]或,STD::原子[医]去取[医]或[医]显式
Defined in header | | |
---|---|---|
| (1) | (since C++11) |
template< class Integral > Integral atomic_fetch_or( std::atomic<Integral>* obj, Integral arg | | |
template< class Integral > Integral atomic_fetch_or( volatile std::atomic<Integral>* obj, Integral arg | | |
| (2) | (since C++11) |
template< class Integral > Integral atomic_fetch_or_explicit( std::atomic<Integral>* obj, Integral arg, std::memory_order order | | |
template< class Integral > Integral atomic_fetch_or_explicit( volatile std::atomic<Integral>* obj, Integral arg, std::memory_order order | |
原子地替换obj
的旧值之间的按位或按位的结果。obj
和arg
返回值obj
以前持有。
执行该操作时,就好像执行了以下操作一样:
1%29obj->fetch_or(arg)
2%29obj->fetch_or(arg, order)
参数
obj | - | pointer to the atomic object to modify |
---|---|---|
arg | - | the value to bitwise OR to the value stored in the atomic object |
order | - | the memory sycnhronization ordering for this operation: all values are permitted. |
返回值
中此函数的效果之前的值。修改顺序成*obj
...
例外
noexcept
规格:
noexcept
可能的实施
模板<class T>TypeName STD::Enable[医]如果<std::is[医]积分<T>*价值&%21 std::is[医]相同<T,bool>::value,T>:type原子[医]去取[医]或%28 STD::原子<T>%2AOBJ,T Arg%29;{返回obj->提取[医]或%28 arg%29;}
*。
例
二次
#include <iostream>
#include <atomic>
#include <thread>
#include <chrono>
#include <functional>
// 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
二次
另见
fetch_or | atomically performs bitwise OR between the argument and the value of the atomic object and obtains the value held previously (public member function of std::atomic) |
---|---|
atomic_fetch_andatomic_fetch_and_explicit (C++11)(C++11) | replaces the atomic object with the result of logical AND with a non-atomic argument and obtains the previous value of the atomic (function template) |
atomic_fetch_xoratomic_fetch_xor_explicit (C++11)(C++11) | replaces the atomic object with the result of logical XOR with a non-atomic argument and obtains the previous value of the atomic (function template) |
C原子文档[医]去取[医]或者,原子[医]去取[医]或[医]显式
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。