相关疑难解决方法(0)

C/C++基本类型是原子的吗?

是C/C++的基本类型,如int,double等,原子,如线程?

他们是否免于数据竞赛; 也就是说,如果一个线程写入这种类型的对象而另一个线程从中读取,那么行为是否定义明确?

如果没有,它是否依赖于编译器或其他东西?

c c++ multithreading atomic

58
推荐指数
4
解决办法
7349
查看次数

如何在C++中声明一个原子向量

我打算声明一个原子变量向量,用作多线程程序中的计数器.这是我尝试过的:

#include <atomic>
#include <vector>

int main(void)
{
  std::vector<std::atomic<int>> v_a;
  std::atomic<int> a_i(1);
  v_a.push_back(a_i);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是gcc 4.6.3的烦人冗长的错误信息:

In file included from /usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++allocator.h:34:0,
             from /usr/include/c++/4.6/bits/allocator.h:48,
             from /usr/include/c++/4.6/vector:62,
             from test_atomic_vec.h:2,
             from test_atomic_vec.cc:1:
/usr/include/c++/4.6/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(__gnu_cxx::new_allocator<_Tp>::pointer, const _Tp&) [with _Tp = std::atomic<int>, __gnu_cxx::new_allocator<_Tp>::pointer = std::atomic<int>*]’:
/usr/include/c++/4.6/bits/stl_vector.h:830:6:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::atomic<int>, _Alloc = std::allocator<std::atomic<int> >, std::vector<_Tp, _Alloc>::value_type = std::atomic<int>]’
test_atomic_vec.cc:10:20:   instantiated from here
/usr/include/c++/4.6/ext/new_allocator.h:108:9: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
/usr/include/c++/4.6/atomic:538:7: error: declared …
Run Code Online (Sandbox Code Playgroud)

c++ stl atomic c++11

33
推荐指数
3
解决办法
2万
查看次数

标签 统计

atomic ×2

c++ ×2

c ×1

c++11 ×1

multithreading ×1

stl ×1