Is pass by reference thread safe?

Discussion in 'C++' started by Andy Chang, Mar 2, 2005.

  1. Andy Chang

    Andy Chang Guest

    Hi,
    If I have this function

    void DoSomething(int& index)
    {
    ::Sleep(10000);
    DoSomethingWith(index);
    }

    Is the variable index thread safe? Meaning that if I call this from two
    different threads, would each thread have its own copy of index?

    Thanks
     
    Andy Chang, Mar 2, 2005
    #1
    1. Advertisements

  2. Andy Chang

    Artie Gold Guest

    Stock answer: C++ has no concepts of threads, so your question is not
    topical here.

    Past that, think about it logically: if multiple threads of execution
    refer to the same variable (as is the case here; remember, the whole
    point of references is that they are aliases to already existing objects).

    HTH,
    --ag
     
    Artie Gold, Mar 2, 2005
    #2
    1. Advertisements

  3. Andy Chang

    Artie Gold Guest

    Stock answer: C++ has no concepts of threads, so your question is not
    topical here.

    Past that, think about it logically: If multiple threads of execution
    refer to the same variable (as is the case here; remember, the whole
    point of references is that they are aliases to already existing
    objects), thread safety is determined by what you're doing in, say,
    DoSomethingWith(), etc.

    HTH,
    --ag
     
    Artie Gold, Mar 2, 2005
    #3

  4. This has not much to do with thread safety itself.


    Threads are off topic in clc++ since so far it is a system-specific
    feature and not part of ISO C++. You will get more help in a newsgroup
    about your platform.


    In a lock based thread environment, and depending on how you have setup
    your threads, above can be thread safe.


    An example of the .NET multithreading model:


    // .NET multithreading thread-lock model
    __gc class SomeClass
    {
    int index;

    //...

    public:

    // ...


    void DoSomething()
    {
    Monitor::Enter(this);

    // Modify index

    Monitor::Exit();
    }

    void DoSomethingElse()
    {
    Monitor::Enter(this);

    // Modify index

    Monitor::Exit();
    }

    // ...
    };


    SomeClass *ps= __gc new SomeClass;

    // ...

    Thread *pthread1= __gc new Thread ( __gc new ThreadStart(ps,
    &SomeClass::DoSomething) );



    Thread *pthread2= __gc new Thread ( __gc new ThreadStart(ps,
    &SomeClass::DoSomethingElse) );


    //Start execution of ps->DoSomething()
    pthread1->Start();

    //Start execution of ps->DoSomethingElse()
    pthread2->Start();

    // ...



    I have no experience with any other thread-style models or
    multithreading in procedural paradigm.


    But in a procedural lock-based model, I can guess you will do something
    like this in your function:


    void DoSomething(int& index)
    {
    Monitor::Enter(SOMETHING); // Or some lock function

    // Modify index

    Monitor::Exit(); // Or some unlock function
    }
     
    Ioannis Vranos, Mar 2, 2005
    #4
  5. Andy Chang

    Andy Chang Guest

    Suppose DoSomething(int& index) is called with different indexes as in

    DoSomething(giThreadAIndex) and DoSomething(giThreadBIndex) from two
    threads, then the function is thread safe even without the monitor calls? I
    mean there is no possibility that the call from thread A could modify thread
    B's index and vice versa?

    Sorry, I didn't know which group to post and this is rather important point
    I need to clear out.

    Thanks
     
    Andy Chang, Mar 2, 2005
    #5
  6. As others have said, threads are OT. However, I think it is safe to say that
    different threads executing DoSomething at the same time will have different
    copies of local variables, including index. So Thread B could conceivably modify
    the int referred to by thread A's copy of index during the execution of
    DoSomething, but not via its own copy of index unless Thread A's index and
    Thread B's happen to refer to the same int.
    Try comp.programming.threads
    Jonathan
     
    Jonathan Turkanis, Mar 2, 2005
    #6


  7. Yes, if the arguments are not related, then the operations are thread safe.


    Which is your platform?
     
    Ioannis Vranos, Mar 2, 2005
    #7
  8. on them
     
    Ioannis Vranos, Mar 2, 2005
    #8
  9. It depends on where index is referencing.

    // this below would unquestionably thread safe.
    void DoSomething(int index)
    {
    ::Sleep(10000);
    DoSomethingWith(index);
    }

    However, since, in your example, index is passed in as a non-const
    reference, it's impossible to tell what it referencing and what
    DoSomethingWith(index) is going to do with it.
     
    Gianni Mariani, Mar 2, 2005
    #9
  10. You are confused.
    A *variable* is never "thread safe".
    Your *code* "thread safe"
    only if two different threads
    cannot modify the same variable.
    If you pass a non-const reference [index]
    to an int variable shared by two or more threads,
    your code is *not* "thread safe"
    since the non-const reference implies that
    the variable can be modified.

    Please note that
    people sometimes refer to code that is actually threaded --
    code that employs mutual exclusion explicitly
    to protect shared variables -- as "thread safe".
     
    E. Robert Tisdale, Mar 2, 2005
    #10
    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.