error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&) 防止原子类型被意外复制

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
 
using namespace std;
atomic<int> i = 0;
 
void iplusplus() {
    int c = 10000000;  //循环次数
    while (c--) {
        i++;
    }
}
int main()
{
    chrono::steady_clock::time_point start_time = chrono::steady_clock::now();//开始时间
    thread thread1(iplusplus);
    thread thread2(iplusplus);
    thread1.join();  // 等待线程1运行完毕
    thread2.join();  // 等待线程2运行完毕
    cout << "i = " << i << endl;
    chrono::steady_clock::time_point stop_time = chrono::steady_clock::now();//结束时间
    chrono::duration<double> time_span = chrono::duration_cast<chrono::microseconds>(stop_time - start_time);
    std::cout << "共耗时:" << time_span.count() << " ms" << endl; // 耗时
    system("pause");
    return 0;
}

g++ -pthread test3.cpp -o t3
test3.cpp:8:17: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
    8 | atomic<int> i = 0;
      |                 ^
In file included from test3.cpp:4:
/usr/include/c++/10/atomic:759:7: note: declared here
  759 |       atomic(const atomic&) = delete;
      |       ^~~~~~
/usr/include/c++/10/atomic:763:17: note:   after user-defined conversion: ‘constexpr std::atomic<int>::atomic(std::atomic<int>::__integral_type)’
  763 |       constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }

 

 编译错误是因为你使用了被删除的拷贝构造函数`std::atomic<int>::atomic(const std::atomic<int>&)`,它用来防止原子类型被意外复制。

上面代码中,你使用了拷贝初始化的语法`atomic<int> i = 0;`,

它相当于`atomic<int> i = atomic<int>(0);`,这就会调用被删除的拷贝构造函数,导致错误

可以使用直接初始化的语法`atomic<int> i(0);`或者列表初始化的语法`atomic<int> i{0};`,它们不会调用拷贝构造函数,而是调用其他的构造函数,如`constexpr atomic(__integral_type __i) noexcept`。

例如,你可以这样修改你的代码:

#include <atomic>
#include <iostream>

int main()
{
    std::atomic<int> i(0); // 直接初始化
    std::atomic<int> j{0}; // 列表初始化
    std::cout << i << " " << j << std::endl;
    return 0;
}

这样就可以解决你的错误,让你的程序正常编译和运行。

 

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
 
using namespace std;
atomic<int> i = {0};//列表初始化,不调用拷贝构造函数
 
void iplusplus() {
    int c = 10000000;  //循环次数
    while (c--) {
        i++;
    }
}
int main()
{
    chrono::steady_clock::time_point start_time = chrono::steady_clock::now();//开始时间
    thread thread1(iplusplus);
    thread thread2(iplusplus);
    thread1.join();  // 等待线程1运行完毕
    thread2.join();  // 等待线程2运行完毕
    cout << "i = " << i << endl;
    chrono::steady_clock::time_point stop_time = chrono::steady_clock::now();//结束时间
    chrono::duration<double> time_span = chrono::duration_cast<chrono::microseconds>(stop_time - start_time);
    std::cout << "共耗时:" << time_span.count() << " ms" << endl; // 耗时
    system("pause");
    return 0;
}

(1) "Use of deleted function" error with std::atomic_int. https://stackoverflow.com/questions/27314485/use-of-deleted-function-error-with-stdatomic-int.
(2) c++ - use of deleted function - std::atomic - Stack Overflow. https://stackoverflow.com/questions/25940654/use-of-deleted-function-stdatomic.
(3) c++ - error: use of deleted function - Stack Overflow. https://stackoverflow.com/questions/5966698/error-use-of-deleted-function.
(4) "Use of deleted function" error with std::atomic_int. https://stackoverflow.com/questions/27314485/use-of-deleted-function-error-with-stdatomic-int.
(5) c++ - use of deleted function - std::atomic - Stack Overflow. https://stackoverflow.com/questions/25940654/use-of-deleted-function-stdatomic.
(6) c++ - error: use of deleted function - Stack Overflow. https://stackoverflow.com/questions/5966698/error-use-of-deleted-function.
(7) error C2280: attempting to reference a deleted function (atomic<int>). https://stackoverflow.com/questions/29332897/error-c2280-attempting-to-reference-a-deleted-function-atomicint.

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aFakeProgramer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值