Advertisements

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?

Click to expand...

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

Advertisements

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?

Click to expand...

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.

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.

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?

Click to expand...

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.

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?

Click to expand...

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.

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?

Click to expand...

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.

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Ask a Question