What is TMonitor in Delphi System unit good for?

后端 未结 1 1874
误落风尘
误落风尘 2020-12-23 20:39

After reading the articles \"Simmering Unicode, bring DPL to a boil\" and \"Simmering Unicode, bring DPL to a boil (Part 2)\" of \"The Oracle at Delphi\" (Allen Bauer), Orac

相关标签:
1条回答
  • 2020-12-23 21:13

    TMonitor combines the notion of a critical section (or a simple mutex) along with a condition variable. You can read about what a "monitor" is here: http://en.wikipedia.org/wiki/Monitor_%28synchronization%29.

    Anyplace you would use a critical section, you can use a monitor. Instead of declaring a TCriticalSection, you can simple create a TObject instance and then use that.

    TMonitor.Enter(FLock);
    try
      // protected code
    finally
      TMonitor.Exit(FLock);
    end;
    

    Where FLock is any object instance. Normally, I just create a TObject:

    FLock := TObject.Create;
    
    0 讨论(0)
提交回复
热议问题