I’m in trouble understanding the real difference between IDSYNC and IDNOTIFY, what means synchronous / asynchronous
in respect to the lines of code I write ?
procedure TForm1.IdTCPServerExecute(AContext: TIdContext);
begin
....
DoSomeThing (TIDNotify) ....
DoSomethingOther(TIDsync) ......
end;
Why can’t I be sure that both lines of code are executed within the TCPServer Execute function?
Is there only the risk that a few lines of code are not executed within my TIDSynfunction or how can a Deadloack be explained ?
TIdSync and TIdNotify accomplish the same goal – to execute a piece of code in the context of the main thread – but they do it in different ways.
TIdSyncis synchronous. TheTIdSync.Synchronize()method blocks the calling thread until after the main thread has called theTIdSync.DoSynchronize()method and it has exited. A deadlock can occur ifTIdSync.Synchronize()is called within a server event handler while the main thread is shutting down that server. This is because the main thread is blocked waiting for the server to terminate its threads. But the thread is blocked waiting for the main thread to process the sync request.TIdNotifyis asynchronous. TheTIdNotify.Notify()method adds theTIdNotify.DoNotify()method into a background queue and exits immediately, so the calling thread is not blocked. The main thread calls theTIdNotify.DoNotify()method at its leisure. There is no deadlock in this situation.