What's the difference between TIdNotify and TIdSync?

 ̄綄美尐妖づ 提交于 2019-12-06 09:05:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!