Thread safety for tiny simple functions
Hello,
I've found that dereferencing elements of an array can be done safely, in parallel, by different threads, as long as the threads are referring to different elements. That is,
Code:
//Thread 1....
for(int i = 0; i < 100; ++i)
array[i] = 3*i;
//end Thread 1
// . . . .
//Thread 2 .....
for(int i = 100; i < 200; ++i)
array[i] = i % 3;
//end Thread 2
is safe. Am I guaranteed the same for the following?
Code:
struct array
{
type& operator()(unsigned int index) { return data[index]; }
// . . .
};
array gyar;
//Thread 1....
for(int i = 0; i < 100; ++i)
gyar(i) = 3*i;
//end Thread 1
// . . . .
//Thread 2 .....
for(int i = 100; i < 200; ++i)
gyar(i) = i % 3;
//end Thread 2
If they're inlined then it's the same. What about if not? Is calling the same function ok? Will one have to wait? I'm looking for an answer in terms of standard behavior (if such a thing exists for threads).
Thanks.