confused about atomic

closed account (N8RzwA7f)
Hi,

just a general question.
I'm trying to get a better general understanding also about the atomic type?
I was looking online at one atomic method , compare_exchange_weak .

it was used with a struct and list pointers. Would there be other ways of using this method, I would like some example then.

does it have to use a pointer to some class/struct?
I tried with atomic of type vectors int , and vector of type atomic int.
as in: std::atomic <std::vector<int>> atom;
or std::vector <std::atomic<int>> vect;

is that even possible /legal code , i'm so confused right now. thanks.
std::atomic< std::vector<int> > atom; is an error; std::vector<int> is not a TriviallyCopyable type.

1
2
3
4
5
6
7
8
9
10
#include <atomic>
#include <vector>

int main()
{
    std::atomic< std::vector<int> > atom ;
    // microsoft: *** error: atomic<T> requires T to be trivially copyable.
    // g++: *** error: static assertion failed: std::atomic requires a trivially copyable type
    // clang++: *** error: _Atomic cannot be applied to type 'std::__1::vector<int, std::__1::allocator<int> >' which is not trivially copyable
}

http://coliru.stacked-crooked.com/a/6108f3f4e6a7b61d
http://rextester.com/CQEQEA92311


std::vector< std::atomic<int> > vect; is fine, but quite pointless; it will always remain as an empty vector.
(We won't be able to resize the vector because std::atomic<T> is not copyable and not movable.)
Topic archived. No new replies allowed.