Solved

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

Posted on 2004-09-10
9
388 Views
Last Modified: 2008-03-17
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
Comment
Question by:alanpong
  • 6
  • 3
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
To add up to " It *might* work" - it might work on a single CPU, but it prone to fail on multiprocessor systems.
0
 
LVL 1

Author Comment

by:alanpong
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
>> 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
 
LVL 1

Author Comment

by:alanpong
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 1

Author Comment

by:alanpong
Comment Utility
the array is designed for read only, no more change later.

what i concern is the local variable.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now