🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Callback, Dll, Thread and synchronization HELP!

#2494
From: Stefano Bonifazi
Date: Thu, 08 Feb 2007 09:08
70 lines
3065 bytes
Hi Mr.Lebeau, Thank U for Ur reply!

Remy Lebeau (TeamB) wrote:
>> I cannot create that queue
> 
> Yes, you can.  Simply have main() call Application->ProcessMessages()
> in a loop for the lifetime of the process.

No I can't sorry, but I cannot modify the console application so much :( 
That is not my code, and it's not written for a Borland compiler at 
all:( Maybe it will be even compiled for Linux (So I should even find a 
way to make my clx code compile in Linux :( .. Check the other post into 
Socket group about indy and CLX )the other programmers r going to use my 
code through my dll. I can do whatever in my dll, but very few 
modifications to the console application...

> You don't.  Not from inside the DLL, anyway.  That is the
> application's responsibility.  Have the DLL call the callback in the
> context of the thread, and then the application has to make sure its
> callback is actually thread-safe.
> 
Ok. I've exported 3 functions from my dll:

bool TryEnterInThreadSafeCode()
{
  if(CritSectInit)
   return TryEnterCriticalSection(&CriticalSection);
  else
   return false;
}

void WaitForEnteringInThreadSafeCode()
{
  if(CritSectInit)
   EnterCriticalSection(&CriticalSection);
}

void LeaveThreadSafeCode()
{
  if(CritSectInit)
   LeaveCriticalSection(&CriticalSection);
}

The Critical Section is initializated in the Initialize function of my 
DLL, and deleted in its Close function (the first and last the user must 
call!)

Then I require the callback function inside the console application to be:

void OnAsynchReceiveCommand(char* Buffer)
{
  WaitForEnteringInThreadSafeCode();
  //do their stuff
  LeaveThreadSafeCode();
}

and anytime in the main thread they require to read memory used by the 
second thread

if(TryEnterInThreadSafeCode())
{
  //do whatever with shared memory
  LeaveThreadSafeCode();
}

All seems to work... is that thread safe?

Thanks again!

Stefano B.
Technical Details
Message-ID: <45cae882@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45ca2bba@newsgroups.borland.com> <45ca3549@newsgroups.borland.com> <45ca373c$1@newsgroups.borland.com> <45ca50ae$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,7631859e1e5bf75d
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Thu, 08 Feb 2007 10:08:26 +0100
From: Stefano Bonifazi <stefboombastic@email.it>
User-Agent: Thunderbird 1.5.0.9 (Windows/20061207)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Callback, Dll, Thread and synchronization HELP!
References: <45ca2bba@newsgroups.borland.com> <45ca3549@newsgroups.borland.com> <45ca373c$1@newsgroups.borland.com> <45ca50ae$1@newsgroups.borland.com>
In-Reply-To: <45ca50ae$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 80.104.231.65
Message-ID: <45cae882@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1170925698 80.104.231.65 (8 Feb 2007 01:08:18 -0800)
Lines: 70
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail