Solved

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

Posted on 2004-09-10
9
409 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
ID: 12029258
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
ID: 12029284
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
ID: 12029299
To add up to " It *might* work" - it might work on a single CPU, but it prone to fail on multiprocessor systems.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:alanpong
ID: 12029411
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
 
LVL 86

Expert Comment

by:jkr
ID: 12029468
>> 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
ID: 12029547
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
ID: 12029597
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
ID: 12029688
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
ID: 12029699
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

809 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