• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

Is it safe for Multithread read-only access on Local variable without CriticalSection?

VC++ 6.0

ONE global class instance only for multithread access.
without critical section to call myfunction(), are the "i" and "sum" safe?
(i know the "array" is safe because it is read-only.)

class myclass{
  myclass(){ array[0] = .... = array[9] = 1; }
  BOOL myfunction()
  {
     int i, sum;
     for(i;i<10;i++)
     sum += array[i];

     if (sum>10)
     return TRUE;
     else return FALSE;
  }

  int array[10];  //change once only in constructor.
}

thanks.
rgds.
alan
--END
0
alanpong
Asked:
alanpong
  • 6
  • 3
1 Solution
 
jkrCommented:
It *might* work, but I would not count on it. But, since you are using plain integers here, I'd suggest  taking a look at the 'Interlocked*()' functions like 'InterlockedExchange()' ('http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/interlockedexchange.asp) which are designed to provide atomic operations for the integer datatype family.
0
 
jkrCommented:
BTW, see also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/synchronization_and_multiprocessor_issues.asp ("Synchronization and Multiprocessor Issues") and http://msdn.microsoft.com/msdnmag/issues/0700/Win32/default.aspx ("Handy Features in Windows, and Interlocked Functions") on this issue.
0
 
jkrCommented:
To add up to " It *might* work" - it might work on a single CPU, but it prone to fail on multiprocessor systems.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
alanpongAuthor Commented:
jkr, thanks for the quick response.
what i understand:
there is only one instance of the class, and the local variables: i and sum is allocated in the SAME FIXED memory area. (am i correct?)

so, i worry, different threads change the i and sum in the SAME memory area, which make them unsafe.

0
 
jkrCommented:
>> there is only one instance of the class, and the local variables
>> so, i worry, different threads change the i and sum in the SAME memory area, which make them unsafe

That's why you should synchronize access in general when using multiple threads. Introducing a strict policy on that will help you save a lot of time and headache in eliminating strange side effects later.
0
 
alanpongAuthor Commented:
my understanding is : a local variable is allocated in fixed memory area according to my knowledge in assembly language.

but i 've not much experience in multithread issue.
(yestersday, i misunderstood each thread will make a copy of the function and also make a copy of the local variables before calling a function)

so, for safe, i'm going to use a criticalsection before call the function.

and thanks for the point about multiprocessor issue.
0
 
jkrCommented:
It's not the local variable that is bothering me, it is the array member. If you are only changing it in the constructor, it is indeed safe to not use any syncing, but once you derive from that class or re-use the code in some other way and make changes to the array at runtime, it might break. That's what I meant with "ntroducing a strict policy on that".
0
 
alanpongAuthor Commented:
the array is designed for read only, no more change later.

what i concern is the local variable.
0
 
jkrCommented:
The local variable is allocated on each thread's stack and that won't interfere with other threads, so this one is "on the safe side" so to speak.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now