问题
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 ?
回答1:
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.
TIdSync
is synchronous. The TIdSync.Synchronize()
method blocks the calling thread until after the main thread has called the TIdSync.DoSynchronize()
method and it has exited. A deadlock can occur if TIdSync.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.
TIdNotify
is asynchronous. The TIdNotify.Notify()
method adds the TIdNotify.DoNotify()
method into a background queue and exits immediately, so the calling thread is not blocked. The main thread calls the TIdNotify.DoNotify()
method at its leisure. There is no deadlock in this situation.
来源:https://stackoverflow.com/questions/13534911/whats-the-difference-between-tidnotify-and-tidsync