I can’t seem to find an answer anywhere on Google. I basically want to know if 2 threads can access normal/member functions like these at the same time and not result in undefined behavior or do I have to use a mutex?
void foo(void)
{
float x(133.7);
float y(10);
std::cout << std::endl << (x * y);
}
void foobar(void)
{
std::cout << std::endl << 1/1;
}
I am pretty sure your code doesn’t have undefined behaviour.
That said, you are using shared data, namely
std::cout.So if you expect
std::cout << std::endl << (x * y)to be executed as a single operation (e.g. to prevent bits of output from different threads getting interleaved on stdout), you are going to have to use locks.