I'm new to C++ multithreading (apparently it's different from python multithreading / multiprocessing since multiple threads can use multiple CPUs inside a single process). I'm aware that a race condition would occur if 2 threads try to change the same data at the same time or if one threads reads something while another is changing it, but I'm not sure if the following situations would require synchronization:
let's say I have the following classes:
class Animal{
public:
string name_;
Animal(string name);
~Animal();
};
class Dog : public Animal{
public:
int price_;
Dog(string name, int price);
~Dog();
};
class Cat : public Animal{
public:
int price_;
Cat(string name, int price);
~Cat();
};
void do_stuff(){
Animal* a = new Dog("Foo", 3);
}
Is it safe to:
- have one thread do a static_cast or a dynamic_cast while another thread is reading or writing to the object?
// thread 1
a->name = "Bar";
// thread 2
Dog* d = static_cast<Dog*>(a);
- have one thread read an attribute of an object while another thread is writing to another attribute of the same object? according the this post (Accessing different data members belonging to the same object from 2 different thread in C++) it seems to be okay but apparently it can cause problems with caching?
Thanks