Larry
Fri Oct 01 19:31:01 CDT 2004
"Ray Mitchell" <RayMitchell@MeanOldTeacher.com> wrote in message news:Q6m7d.599$M05.295@newsread3.news.pas.earthlink.net...
> Hello,
Hi.
> I'm not very familiar with the intricacies of critical sections. In the function below my goal is to increment counter with each
> call and return that incremented value. However, it appears to me that if a second thread calls myFunction before the call from
> the first thread gets finished doing the ...other things..., counter might end up getting incremented twice before its value gets
> returned to the first thread.
Assuming that cs has been initialized (by InitializeCriticalSection()),
the code you show would block at the EnterCriticalSection() call
if another thread had passed that point but not yet reached the
LeaveCricticalSection() call. That is the whole purpose of having
and using critical sections. I would recommend that you read
some of the documentation Microsoft provides for the APIs
that support multithreaded programming. You could star here:
http://msdn.microsoft.com/library/en-us/dllproc/base/synchronization.asp
> The value of counter needs to be maintained between calls. Is there a more appropriate way to do this?
If you really just ned to manipulate a counter, then you should
probably use the InterlockedX set of calls, where X can be
Increment, Decrement and other useful things.
> Thanks,
You are welcome.
> Ray Mitchell
>
>
> CRITICAL_SECTION cs;
> int myFunction()
> {
> EnterCriticalSection(&cs);
>
> static int counter;
>
> ++counter;
>
> LeaveCriticalSection(&rtcCriticalSection);
>
> ... other things ...
>
> return counter;
> }
--
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.