🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #780

Re: threads and critical sections

#780
From: Clayton Arends
Date: Wed, 09 Jan 2008 17:55
69 lines
2862 bytes
"mataus" <mataus@gmail.com> wrote in message 
news:4784d07b@newsgroups.borland.com...

> I have a question about threads and Critical Sections.  I need my main
> thread to tell another thread of some state change.  This is shows with an
> integer or boolean in the second thread.  Must i use Critical Section when
> updating or is it safe to change an integer value?

Always use a critical section or some other semaphore for data that can be 
accessed from multiple threads.  You should encapsulate the critical section 
inside the thread that owns the variable.  For example:

  class TThread1
  {
    private:
      TCriticalSection* fSync;
      bool fSomeState;

      ...

      bool get_SomeState();
      void set_SomeState(bool Value);

      ...

      void SomeInternalMethod();

    public:
      ...

      __property SomeState =
        { read = get_SomeState, write = set_SomeState };
  };

  bool TThread1::get_SomeState()
  {
    bool result;
    fSync->Acquire();
    result = fSomeState;
    fSync->Release();
    return result;
  }

  void TThread1::set_SomeState(bool Value)
  {
    fSync->Acquire();
    fSomeState = Value;
    fSync->Release();
  }

  void TThread1::SomeInternalMethod()
  {
    // The thread should always access the variable via the semaphore
    //  not directly.
    if (SomeState)
    {
    }
  }

Notes:

Since a semaphore is not tied to any specific memory location a single one 
can be used to protect access to multiple variables (if needed).

To increase efficiency for reads use a semaphore like 
TMultiReadExclusiveWrite so that multiple inspection requests are not 
blocked unless there is a write happening.

Clayton 
Technical Details
Message-ID: <47850ad6@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.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
References: <4784d07b@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,5d08ceba3672bae6
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Clayton Arends" <nospam_claytonarends@hotmail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4784d07b@newsgroups.borland.com>
In-Reply-To: <4784d07b@newsgroups.borland.com>
Subject: Re: threads and critical sections
Date: Wed, 9 Jan 2008 09:55:48 -0800
Lines: 70
MIME-Version: 1.0
Content-Type: text/plain;
 format=flowed;
 charset="Windows-1252";
 reply-type=response
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Windows Mail 6.0.6000.16480
X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6000.16545
NNTP-Posting-Host: 68.113.27.46
Message-ID: <47850ad6@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1199901398 68.113.27.46 (9 Jan 2008 09:56:38 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.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