question about objects by reference and threadsafety

Discussion in 'C++' started by john smith, Jan 21, 2005.

  1. john smith

    john smith Guest

    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?

    }
     
    john smith, Jan 21, 2005
    #1
    1. Advertisements

  2. john smith

    shez Guest

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

    -shez-
     
    shez, Jan 21, 2005
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.