question about objects by reference and threadsafety

J

john smith

If I create an object and pass it by reference to two other functions, and
the other two function have separate threads
must I worry about threadsafety? The two functions could modify the myCar
object in main at the same time couldn't they?

I have trouble understading if this is a problem or not. both functions
reference the myCar object in main and could potentially create a critical
section
path correct?


Thanks.

For example

int main()
{

Car myCar;

fixCar( myCar ); // fixCar() is a process running on its
own thread

fixCarAgain( myCar ); // fixCarAgain() is a process running on its
own thread

return 0;

}

void fixCar( Car& myCar ) // separate thread
{
// if I modify myCar should I do mutual exclusion semaphore here?

}

void fixCarAgain( Car& myCar ) // separate thread
{
// if I modify myCar should I do mutual exclusion semaphore here?

}
 
S

shez

john said:
int main()
{

Car myCar;

fixCar( myCar ); // fixCar() is a process running on its
own thread

fixCarAgain( myCar ); // fixCarAgain() is a process running on its
own thread

return 0;

}

void fixCar( Car& myCar ) // separate thread
{
// if I modify myCar should I do mutual exclusion semaphore here?

}

void fixCarAgain( Car& myCar ) // separate thread
{
// if I modify myCar should I do mutual exclusion semaphore here?

}

Yes, you will need a mutex if you modify myCar.

-shez-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top