Re:Main Thread reaction to TThread.Synchronize()
I appreciate you guys helping me out, but...
Hmmm... now I am really confused.
The programming problem which lead up to my question was the following:
- I have a Delphi app which uses a C++ DLL which handles blocking TCP/IP
sockets to communicate with an external slave unit. For example, when
I use a DLL function call SendCommandString(pCommand,pResponse), the
command contained in the PChar pCommand is sent out over the socket and
the DLL holds out until an ASCII response string is received from the other
side
(with a certain timeout, of course). Now the intention is that no other
commands
shall be sent out while we are still waiting for response to the first
command,
so there is a mutex mechanism in the DLL which prevents other threads
to execute commands through the DLL until the current function call has
completed.
- In my Application I have a couple of thread-driven timers which are
responsible
for generating periodic update commands to the slave unit. If I use
Synchronize()
on the timer event handler, the call is executed from the Main VCL thread
and as
a natural consequence the mutex mechanism in the DLL then doesn't work;
What DID baffle me was that apparently even though the Main Thread was
already
busy executing a DLL call of the SendCommandString type at the time when
the
thread timer kicked in, a second call would be allowed immediately when
Synchronize() was used (thus interferring with the ongoing Main Thread
call), so
our solution was to NOT use Synchronize(), which made the Mutex mechanism
in the DLL take effect.
The observation above indicates that Martin Larsson is right... and which
is why
I have become puzzled about the real workings of Synchronize().
So if Martin is right, and SendMessage immediately calls WndProc of the
Main window
(=Main VCL Thread, "most of the time", how is this defined?), it would seem
that Synchronize is indeed NOT a guarantee for thread-safe operation!
Kristofer Skaug
kristo...@skaug.demon.nl
David Rifkind <drifk...@acm.deleteme.org> wrote in article
<35e22780.29433...@netnews.worldnet.att.net>...
Quote
> On Mon, 24 Aug 1998 23:51:00 +0200, martin.lars...@delfidata.no.NOSPAM
> (Martin Larsson) wrote:
> >In article <903991260.29123.2.rover.c29fe...@news.demon.nl>, "Zweitze de
> >Vries" <zweitze.at.iname.com> ("Zweitze de Vries"
<zweitze.at.iname.com>)
Quote
> >says...
> >> The main thread will finish its current business first. The
synchronized
> >> function will be called when the main thread enters the message loop.
This
> >> is also the case when the main thread calls
Application.ProcessMessages.
Quote
> >Actually, no. At least not in Delphi 3. The TThread.Synchronize method
> >uses SendMessage. SendMessage immediately calls the WndProc of the given
> >window, and does not return until processing of the message is done.
> Only within the same thread. When you send a message to another thread,
> it doesn't get delivered until the receiving thread retrieves messages.
> >The handler for CM_EXECPROC simply calls the desired function directly.
> >M.
> --
> Report all {*word*201} mail to your Potsmaster.