atomic<int> 使用示例
时间: 2023-12-20 10:04:47 浏览: 31
下面是一个使用std::atomic<int>的示例:
```c++
#include <iostream>
#include <atomic>
#include <thread>
std::atomic<int> counter(0);
void incrementCounter() {
for (int i = 0; i < 1000000; ++i) {
counter++;
}
}
int main() {
std::thread t1(incrementCounter);
std::thread t2(incrementCounter);
t1.join();
t2.join();
std::cout << "Counter value: " << counter << std::endl;
return 0;
}
```
这个示例创建了两个线程,每个线程都会对counter进行1000000次自增操作。由于std::atomic<int>保证了原子性,因此不会出现数据竞争的问题。最终输出的counter值应该是2000000。
相关问题
std::atomic<int> 举例使用
下面是一个使用 std::atomic<int> 的示例代码:
```cpp
#include <iostream>
#include <thread>
#include <atomic>
std::atomic<int> counter(0);
void increment_counter() {
for (int i = 0; i < 1000000; ++i) {
++counter; // 自增操作会自动转换为原子操作
}
}
int main() {
std::thread t1(increment_counter);
std::thread t2(increment_counter);
t1.join();
t2.join();
std::cout << "Final counter value is " << counter << std::endl; // 原子地读取计数器的最终值
return 0;
}
```
在这个例子中,我们同样创建了一个 std::atomic<int> 类型的计数器,并在两个线程中并发地对它进行自增操作。由于 std::atomic<int> 已经重载了自增运算符,因此我们可以直接使用 ++counter 进行自增操作,它会自动转换为原子操作。最后,我们同样使用 counter 变量来读取计数器的最终值,并输出到控制台上。
std::atomic<std::shared_ptr<T>>示例
std::atomic<std::shared_ptr<T>>是一个原子共享指针,可以在多线程环境下安全地访问和修改共享资源。以下是一个示例:
```
#include <atomic>
#include <iostream>
#include <memory>
#include <thread>
class Foo {
public:
Foo() { std::cout << "Foo constructor" << std::endl; }
~Foo() { std::cout << "Foo destructor" << std::endl; }
void bar() { std::cout << "Foo bar" << std::endl; }
};
std::atomic<std::shared_ptr<Foo>> ptr;
void thread_func() {
std::shared_ptr<Foo> p(new Foo);
ptr.store(p);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::shared_ptr<Foo> q = ptr.load();
q->bar();
}
int main() {
std::thread t(thread_func);
std::shared_ptr<Foo> r = ptr.load();
if (r) {
r->bar();
}
t.join();
return 0;
}
```
在这个示例中,我们创建了一个名为Foo的类,并在其中定义了一个名为bar的方法。然后,我们使用std::atomic<std::shared_ptr<Foo>>定义了一个原子共享指针ptr。在thread_func函数中,我们创建了一个指向Foo对象的共享指针p,并使用ptr.store(p)将其存储到ptr中。然后,我们等待1秒钟,使用ptr.load()获取ptr中存储的共享指针q,并调用它的bar方法。
在主函数中,我们使用ptr.load()获取ptr中存储的共享指针r,并调用它的bar方法。最后,我们使用t.join()等待线程t的结束。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)