🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Affecting a TThread owned variable

#9565
From: Clayton Arends
Date: Sun, 04 Jul 2004 17:12
77 lines
2809 bytes
"Ron Sawyer" <XXXRSAWYER8128XQQXX@aol.com> wrote in message
news:40e80626@newsgroups.borland.com...

> What must I do to make sure that I am adding things in a
> thread-safe manner?
>
> What must I do to make sure that I am removing things in a
> thread-safe manner?

The code might look something like this:

  // Thread .H file
  #include <SysUtils.hpp>

  class TMyThread : public TThread
  {
    private:
      TMultiReadExclusiveWriteSynchronizer* fQueueSync;

      void ProcessElement();

    public:
      TMyThread();
      virtual __fastcall ~TMyThread();

      void AddStruct(const TSomeData& SomeData);
      void DelStruct(const TSomeData& SomeData);
  };


  // Thread .CPP file
  TMyThread::TMyThread() : TThread(false)
  {
    fQueueSync = new TMultiReadExclusiveWriteSynchronizer;
  }

  __fastcall TMyThread::~TMyThread()
  {
    delete fQueueSync;
  }

  void TMyThread::AddStruct(const TSomeData& SomeData)
  {
    fQueueSync->BeginWrite();
    // add to the queue
    fQueueSync->EndWrite();
  }

  void TMyThread::DelStruct(const TSomeData& SomeData)
  {
    fQueueSync->BeginRead();
    // find the element in the queue
    if (found)
    {
      fQueueSync->BeginWrite();
      // delete element from the queue
      fQueueSync->EndWrite();
    }
    fQueueSync->EndRead();
  }

  void TMyThread::ProcessElement()
  {
    fQueueSync->BeginRead();
    // copy the element off the top of the queue
    fQueueSync->EndRead();
    // process the element and signal the form
  }

A word about DelStruct.  This method of changing the lock mode from Read to
Write works in BCB6 (I think BCB5 also).  In earlier versions it wasn't
guaranteed to work.  It can also be written using just a single write lock.
It would be a very, very small performance hit to have it that way (unless
your queue can be several million elements long).

HTH,
- Clayton
Technical Details
Message-ID: <40e83a7e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40e80626@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,de47f0217318ee58
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Clayton Arends" <nospam_claytonarends@hotmail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <40e80626@newsgroups.borland.com>
Subject: Re: Affecting a TThread owned variable
Date: Sun, 4 Jul 2004 10:12:22 -0700
Lines: 79
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 204.210.28.15
Message-ID: <40e83a7e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1088961150 204.210.28.15 (4 Jul 2004 10:12:30 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail