lundi 23 mai 2016

C++11: can we create `std::shared_ptr` type `std::atomic` variable

I am trying to create a std::shared_ptr type std::atomic variable. compile on online compiler

#include <iostream>
#include <memory>
#include <atomic>
//using namespace std;

class abc
{
public:
std::atomic <std::shared_ptr <int>> abl;
std::shared_ptr<int> a;  

void set(int x)
{
    abl.store(std::make_shared<int>(10) , std::memory_order_relaxed);
}

void out()
{
    a = abl.load(std::memory_order_relaxed);
    std::cout<< "value" << *a;
}
};

int main()
{
    abc ab;
    ab.set(10);
    ab.out();
    std::cout << "Hello World"; 

    return 0;
}

but getting following error

error: static assertion failed: std::atomic requires a trivially copyable type                                              
       static_assert(__is_trivially_copyable(_Tp), 

but when i tried it on my linux machine with complier version gcc 4.8(C++11) it compiled successfully but crashed.

i go through this and found shared_ptr is surely not TriviallyCopyable. compiler should give compile time error

Aucun commentaire:

Enregistrer un commentaire