如何使用std :: atomic<> [英] how to use std::atomic<>

查看:180
本文介绍了如何使用std :: atomic<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,我想使用它在不同的线程,我想我可以使用std ::原子如:

  classs A 
{
int x;

public:
A()
{
x = 0;
}

void Add()
{
x ++;
}

void Sub()
{
x--
}
};

,在我的代码中:

  std :: atomic< A>一个; 

和不同的线程:

  a.Add(); 

  a.Sub(); 

但是当我收到错误a.Add



编辑1

如何实现这一目标? >

请注意,这是一个示例,我想要确保对A类的访问是线程安全的,所以我不能使用

  std :: atomic< int> X; 

如何使用std :: atomic使类线程安全?

解决方案

你需要使 x 属性原子,而不是你的整个类,如下: / p>

  class A 
{
std :: atomic< int> X;

public:
A(){
x = 0;
}
void Add(){
x ++;
}
void Sub(){
x--;
}
};

您在原始代码中获得的错误是完全正常的:没有 std :: atomic< A> :: Add 方法(请参阅此处< a>),除非您为 std :: atomic< A> 提供专业化。



编辑:您不能通过使用 std :: atomic A类线程安全c>。为了使它的线程安全,你可以使其属性原子性(如上所述,并提供标准库给它一个专门化),或使用互斥锁自己锁定你的资源。请参阅互斥标头。例如:

  class A 
{
std :: atomic< int& X;
std :: vector< int> v;
std :: mutex mtx;

void Add(){
x ++;
}
void Sub(){
x--;
}

/ *保护向量的示例方法* /
void complexMethod(){
mtx.lock();

//做任何你需要的复杂操作
// - 访问元素
// - 擦除元素
// - etc ...

mtx.unlock();
}

/ *
**另一个使用std :: lock_guard的例子,如注释中的建议
**如果你不需要手动操作互斥
* /
void complexMethod2(){
std :: lock_guard< std :: mutex> guard(mtx);

//访问,擦除,添加元素...
}

};


I have a class that I wan to use it in different threads and I think I may be able to use std::atomic such as this:

classs A
{
    int x;

public:
    A()
    {
        x=0;
    }

    void Add()
    {
        x++;
    }

    void Sub()
    {
        x--;
    }     
};

and in my code:

  std::atomic<A> a;

and in different thread:

  a.Add();

and

  a.Sub();

but when I am getting error that a.Add() is not known. How can i achieve this?

Is there any better way to do this?

Edit 1

Please note that it is a sample example, and what I want is to make sure that access to class A is threadsafe, so I can not use

std::atomic<int> x;

How can I make a class thread safe using std::atomic ?

解决方案

You need to make the x attribute atomic, and not your whole class, as followed:

class A
{
    std::atomic<int> x;

    public:
      A() {
        x=0;
      }
      void Add() {
        x++;
      }
      void Sub() {
        x--;
      }     
};

The error you get in you original code is completely normal: there is no std::atomic<A>::Add method (see here) unless you provide a specialization for std::atomic<A>.

Referring your edit: you cannot magically make your class A thread safe by using it as template argument of std::atomic. To make it thread safe, you can make its attributes atomic (as suggested above and provided the standard library gives a specialization for it), or use mutexes to lock your ressources yourself. See the mutex header. For example:

class   A
{
  std::atomic<int>      x;
  std::vector<int>      v;
  std::mutex            mtx;

  void  Add() {
    x++;
  }
  void  Sub() {
    x--;
  }

  /* Example method to protect a vector */
  void  complexMethod() {
    mtx.lock();

    // Do whatever complex operation you need here
    //  - access element
    //  - erase element
    //  - etc ...

    mtx.unlock();
  }

  /*
  ** Another example using std::lock_guard, as suggested in comments
  ** if you don't need to manually manipulate the mutex
  */
  void  complexMethod2() {
    std::lock_guard<std::mutex> guard(mtx);

    // access, erase, add elements ...
  }

};

这篇关于如何使用std :: atomic&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆