🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by mataus Wed, 09 Jan 2008 13:56
threads and critical sections
#778
Author: mataus
Date: Wed, 09 Jan 2008 13:56
10 lines
1135 bytes
Hello all - and good new year.

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?

Many thanks

mataus 
Re: threads and critical sections
#779
Author: danzer
Date: Wed, 09 Jan 2008 14:23
17 lines
1428 bytes
mataus wrote:
> Hello all - and good new year.
> 
> 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?
> 
> Many thanks
> 
> mataus 
> 
> 

Use the API functions InterlockedIncrement and InterlockedDecrement.

Danzer
Re: threads and critical sections
#780
Author: 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 
Re: threads and critical sections
#730
Author: Goran Ekstrom
Date: Sun, 20 Jan 2008 18:48
5 lines
1190 bytes
> 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?

Check out the "On atomic access (follow-up to 'The standard on assignment')" 
in the cppbuilder.language.cpp group, posted 15:th of December 2007.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads