概述
C++中原子变量(atomic)是一种多线程编程同步机制,它能够确保对共享变量的操作在执行时不会被其他线程的操作干扰,atomic是提供一种生成原子操作数的一种机制,避免竞态条件(race condition)和死锁(deadlock)等问题。
说明
数组初始化
for (int i = 0; i < 10; i++) {
data.emplace_back(i);
}
在容器的末尾插入元素
emplace_back
定义原子变量
std::atomic<size_t> shared_index{0};
原子变量累加
size_t worker_index = shared_index.fetch_add(1);
其他函数(摘自网络)
is_lock_free(example)
#include <iostream>
#include <atomic>
int main()
{
std::atomic<int> a;
// 显示 true 或 false,而不是 1 或 0
std::cout << std::boolalpha
<< "std::atomic<int> is "
<< (a.is_lock_free() ? "" : "not ")
<< "lock-free\n";
return 0;
}
store(example)
#include <iostream>
#include <atomic>
int main()
{
std::atomic<int> atomic_int(0);
int val = 10;
atomic_int.store(val);
std::cout << "Value stored in atomic object: " << atomic_int << std::endl;
return 0;
}
load(example)
std::atomic<int> foo (0);
int x;
do {
x = foo.load(std::memory_order_relaxed); // get value atomically
} while (x==0);
exchange(example)
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector
std::atomic<bool> ready (false);
std::atomic<bool> winner (false);
void count1m (int id) {
while (!ready) {} // wait for the ready signal
for (int i=0; i<1000000; ++i) {} // go!, count to 1 million
if (!winner.exchange(true)) { std::cout << "thread #" << id << " won!\n"; }
};
int main ()
{
std::vector<std::thread> threads;
std::cout << "spawning 10 threads that count to 1 million...\n";
for (int i=1; i<=10; ++i) threads.push_back(std::thread(count1m,i));
ready = true;
for (auto& th : threads) th.join();
return 0;
}
compare_exchange_weak(example)
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector
// a simple global linked list:
struct Node { int value; Node* next; };
std::atomic<Node*> list_head (nullptr);
void append (int val) { // append an element to the list
Node* oldHead = list_head;
Node* newNode = new Node {val,oldHead};
// what follows is equivalent to: list_head = newNode, but in a thread-safe way:
while (!list_head.compare_exchange_weak(oldHead,newNode))
newNode->next = oldHead;
}
int main ()
{
// spawn 10 threads to fill the linked list:
std::vector<std::thread> threads;
for (int i=0; i<10; ++i) threads.push_back(std::thread(append,i));
for (auto& th : threads) th.join();
// print contents:
for (Node* it = list_head; it!=nullptr; it=it->next)
std::cout << ' ' << it->value;
std::cout << '\n';
// cleanup:
Node* it; while (it=list_head) {list_head=it->next; delete it;}
return 0;
}
测试代码
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
std::atomic<size_t> shared_index{0};
std::vector<int> data;
void worker(int index, int timeout) {
while(true) {
size_t worker_index = shared_index.fetch_add(1);
if (worker_index >= data.size()) {
break;
}
std::cout << "Worker " << index << " handles " << worker_index << std::endl;
data[worker_index] = data[worker_index] * 2;
// 延时函数
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
}
}
int main() {
for (int i = 0; i < 10; i++) {
data.emplace_back(i);
}
std::thread worker1(worker, 1, 50);
std::thread worker2(worker, 2, 20);
worker1.join();
worker2.join();
std::cout << "Result: ";
for (auto& v : data) {
std::cout << v << ' ';
}
std::cout << std::endl;
}
我公司承接各类技术服务,主要聚焦于:stm32、单片机、嵌入式、QT应用开发、Web+Python+Django应用开发。欢迎合作。