Article View: borland.public.cppbuilder.nativeapi
Article #7539Re: Accessing same variable from multiple threads
From: Vladimir Afanasyev
Date: Wed, 19 Jan 2005 10:32
Date: Wed, 19 Jan 2005 10:32
32 lines
1934 bytes
1934 bytes
Hi,
"Tumurbaatar S." <nospam_tumur@magicnet.mn> wrote:
>There's a integer variable visible to 2 threads. One of the threads
>reads/modifies this var, but other thread only reads it.
>But an exact value of the var is not important for the 2nd thread.
>The thread checks it for zero or not, i.e. "if (my_var)...".
>Is it not matter that I'm not using any synchronization methods?
If both threads from a single process you can use
critical section objects for accessing to your variable.
But in this case all access to variable must be in single
critical section.
EnterCriticalSection
switch(threadNum)
{
case 1: {.... read variable.... } break;
case 2: {.... read/modify variable ...} break;
}
LeaveCriticalSection
For details see help.
More common way - using semaphore object. The threads query
access (for example, WaitForSingleObject) before access variable (or another sharing resource).
More information - in Win32 Programmer's Reference Help
in Part "Interprocess Synchronization".
Vladimir.
Technical Details
Message-ID:
<41ee3727$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<41edea6b@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: "Vladimir Afanasyev" <avv@systema.kiev.ua> Sender: "Vladimir Afanasyev" <avv@systema.kiev.ua> Reply-To: "Vladimir Afanasyev" <avv@systema.kiev.ua> Subject: Re: Accessing same variable from multiple threads Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 83.218.237.46 X-Forwarded-For: unknown References: <41edea6b@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <41ee3727$1@newsgroups.borland.com> Date: 19 Jan 2005 02:32:07 -0800 X-Trace: newsgroups.borland.com 1106130727 207.105.83.66 (19 Jan 2005 02:32:07 -0800) Lines: 32 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail