Is pass by reference thread safe?

A

Andy Chang

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
 
A

Artie Gold

Andy said:
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?
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
 
A

Artie Gold

Andy said:
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?
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
 
I

Ioannis Vranos

Andy said:
Hi,
If I have this function

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


This has not much to do with thread safety itself.

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?


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
}
 
A

Andy Chang

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
 
J

Jonathan Turkanis

Andy said:
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?

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.
Sorry, I didn't know which group to post and this is rather important
point I need to clear out.

Try comp.programming.threads

Jonathan
 
I

Ioannis Vranos

Andy said:
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?



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

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


Which is your platform?
 
G

Gianni Mariani

Andy said:
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?

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.
 
E

E. Robert Tisdale

Andy said:
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?

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".
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top