I am new to multithread programming, please don’t laugh if this seems too simple.
I have a simple class object which has several member variables, e.g.,
class XYZ
{
public:
int a;
int b;
...
};
xyz is a shared objects among multiple threads, what’s the best way to share “a” among multiple threads? For example,
In thread A, I will do something like
xyz.a = xyz.a + rand();
In thread B, I will do something like
xyz.a = xyz.a - rand();
I heard about create a mutex as a class member, can someone please give a simple example how to do that.
How can I create a class interface which allows multiple threads to safely access its member?
You can use a mutex. They can be locked by only one thread simultaneously.
Then use for the thread code