Article View: borland.public.cppbuilder.nativeapi
Article #7568Re: Accessing same variable from multiple threads
From: Tumurbaatar S.
Date: Thu, 20 Jan 2005 04:35
Date: Thu, 20 Jan 2005 04:35
55 lines
2456 bytes
2456 bytes
Yes, I use synch methods when an exact value of shared var is needed.
But my question was about checking for zero or not zero only.
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:41ee9aae$1@newsgroups.borland.com...
>
> "Tumurbaatar S." <nospam_tumur@magicnet.mn> wrote in message
> news:41edea6b@newsgroups.borland.com...
>
>> Is it not matter that I'm not using any synchronization methods?
>
> Yes, it does matter. Always synchronize accees to any data that is shared
> by multiple threads, especially if one of the threads is altering the
> data.
> In this particular case, the easiest way to synchronize access to a shared
> integer is to use the Interlocked functions, such as InterlockedExchange
> and
> InterlockedExchangeAdd():
>
>
> int my_var = 0;
>
> --- thread1 ---
> InterlockedExchange(&my_var, new_value);
>
>
> --- thread2 ---
> if( InterlockedExchangeAdd(&my_var, 0) != 0 ) ...
>
>
> Otherwise, use a critical section instead:
>
> int my_var = 0;
> CRITICAL_SECTION cs = {0};
>
> --- program startup ---
> InitializeCriticalSection(&cs);
>
> --- program termination ---
> DeleteCriticalSection(&cs);
>
> --- thread1 ---
> EnterCriticalSection(&cs)
> my_var = new_value;
> LeaveCriticalSection(&cs)
>
> --- thread2 ---
> EnterCriticalSection(&cs);
> if( my_var != 0 ) ...
> LeaveCriticalSection(&cs);
>
>
> Gambit
>
>
Technical Details
Message-ID:
<41ef3506@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41edea6b@newsgroups.borland.com> <41ee9aae$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,4809cdd9a7828fa7 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Tumurbaatar S." <nospam_tumur@magicnet.mn> Newsgroups: borland.public.cppbuilder.nativeapi References: <41edea6b@newsgroups.borland.com> <41ee9aae$1@newsgroups.borland.com> Subject: Re: Accessing same variable from multiple threads Date: Thu, 20 Jan 2005 12:35:14 +0800 Lines: 57 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2527 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527 NNTP-Posting-Host: 202.131.2.6 Message-ID: <41ef3506@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1106195718 202.131.2.6 (19 Jan 2005 20:35:18 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail