Solved

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

Posted on 2004-09-10
9
424 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
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.

730 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