I have a class that I want to use in different threads and I think I may be able to use std::atomic
this way:
class A
{
int x;
public:
A()
{
x=0;
}
void Add()
{
x++;
}
void Sub()
{
x--;
}
};
and in my code:
std::atomic<A> a;
and in a different thread:
a.Add();
and
a.Sub();
but I am getting an error that a.Add()
is not known. How can I solve this?
Is there any better way to do this?
Please note that it is an example, and what I want is to make sure that access to class A is thread-safe, so I can not use
std::atomic<int> x;
How can I make a class thread-safe using std::atomic
?