Solved

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

Posted on 2004-09-10
9
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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