TMonitor vs TRTLCriticalSection - DelphiTools.info


http://delphitools.info/2013/06/06/tmonitor-vs-trtlcriticalsection/

Comments

  1. Searched source base: No TMonitor found - phew!

    But - where in the RTL/VCL is it used - and to what effect?

    ReplyDelete
  2. It was mentioned as being the lock mechanism for the infamous global lock for the weak references global hash in NextGen...

    Haven't scouted the RTL for it beyond XE (where its other methods are plagued by bugs).

    Apart from that, TMonitor is costing you SizeOf(Pointer) per object, plus a couple cycles in object creation/destruction, even if you never ever use it.

    ReplyDelete
  3. XE3
    Searching for: TMonitor - C:\Program Files (x86)\Embarcadero\RAD Studio\10.0\source\
    data\bde\Bde.DBTables.pas: 12
    data\cloud\Data.Cloud.CloudAPI.pas: 6
    data\datasnap\Data.DBXMessageHandlerJSonServer.pas: 4
    data\datasnap\Datasnap.DSClientRest.pas: 14
    data\datasnap\Datasnap.DSCommonServer.pas: 10
    data\datasnap\Datasnap.DSHTTPCommon.pas: 26
    data\datasnap\Datasnap.DSPlatform.pas: 14
    data\datasnap\Datasnap.DSServer.pas: 51
    data\datasnap\Datasnap.DSSession.pas: 26
    data\dbx\Data.DBXCommon.pas: 9
    data\dbx\Data.DBXJSONReflect.pas: 16
    data\dbx\Data.DBXOpenSSL.pas: 10
    data\dsnap\Datasnap.DataBkr.pas: 4
    data\dsnap\Datasnap.DSIntf.pas: 2
    data\dsnap\HTTPIntr.pas: 4
    fmx\FMX.Platform.Mac.pas: 6
    fmx\FMX.Platform.Win.pas: 2
    indy\implementation\IPPeerServer.pas: 12
    rtl\common\System.Classes.pas: 14
    rtl\common\System.Generics.Collections.pas: 16
    rtl\common\System.SyncObjs.pas: 24
    rtl\osx\Macapi.ObjectiveC.pas: 8
    rtl\sys\System.pas: 51
    rtl\sys\System.SysUtils.pas: 1
    ToolsAPI\DesignWindows.pas: 2
    vcl\Vcl.ActnMenus.pas: 1
    vcl\Vcl.ComCtrls.pas: 1
    vcl\Vcl.Controls.pas: 1
    vcl\Vcl.Dialogs.pas: 1
    vcl\Vcl.FileCtrl.pas: 1
    vcl\Vcl.Forms.pas: 37
    vcl\Vcl.OleCtrls.pas: 1
    vcl\Vcl.RibbonActnMenus.pas: 1
    vcl\Vcl.StdActns.pas: 1
    Found 389 occurrence(s) in 34 file(s)


    XE4:
    Searching for: TMonitor - C:\Program Files (x86)\Embarcadero\RAD Studio\10.0\source\
    data\bde\Bde.DBTables.pas: 12
    data\cloud\Data.Cloud.CloudAPI.pas: 6
    data\datasnap\Data.DBXMessageHandlerJSonServer.pas: 4
    data\datasnap\Datasnap.DSClientRest.pas: 12
    data\datasnap\Datasnap.DSCommon.pas: 18
    data\datasnap\Datasnap.DSCommonServer.pas: 14
    data\datasnap\Datasnap.DSHTTPCommon.pas: 8
    data\datasnap\Datasnap.DSPlatform.pas: 14
    data\datasnap\Datasnap.DSServer.pas: 51
    data\datasnap\Datasnap.DSSession.pas: 26
    data\dbx\Data.DBXCommon.pas: 11
    data\dbx\Data.DBXJSONReflect.pas: 24
    data\dbx\Data.DBXOpenSSL.pas: 10
    data\dsnap\Datasnap.DataBkr.pas: 4
    data\dsnap\Datasnap.DSIntf.pas: 2
    data\dsnap\HTTPIntr.pas: 4
    fmx\FMX.Controls.pas: 4
    fmx\FMX.Platform.iOS.pas: 6
    fmx\FMX.Platform.Mac.pas: 6
    indy\implementation\IPPeerServer.pas: 12
    rtl\common\System.Classes.pas: 14
    rtl\common\System.Generics.Collections.pas: 16
    rtl\common\System.SyncObjs.pas: 24
    rtl\osx\Macapi.ObjectiveC.pas: 8
    rtl\sys\System.pas: 51
    rtl\sys\System.SysUtils.pas: 1
    ToolsAPI\DesignWindows.pas: 2
    vcl\Vcl.ActnMenus.pas: 1
    vcl\Vcl.ComCtrls.pas: 1
    vcl\Vcl.Controls.pas: 1
    vcl\Vcl.Dialogs.pas: 1
    vcl\Vcl.FileCtrl.pas: 1
    vcl\Vcl.Forms.pas: 37
    vcl\Vcl.OleCtrls.pas: 1
    vcl\Vcl.RibbonActnMenus.pas: 1
    vcl\Vcl.StdActns.pas: 1
    Found 409 occurrence(s) in 36 file(s)

    I guess it can sort out some questions around the performance of DataSnap !  At least someone has seen the light for one of the units in XE4.

    ReplyDelete
  4. After trying to get some extra speed using the TMultiReadExclusiveWriteSynchronizer or whatever it was called back in the days, I came to the conclusion that for most multithreaded code, critical sections were the best choice.

    Not only was the MREW beast considerably slower than just using critical sections, even with 10-20 to 1 read/write ratio on quad core (well socket back then), but QC revealed a lot of bugs in it. Note that this isn't just MREW in Delphi, Boost's MREW implementation seems to be flakey as well.

    So, after that I very much advocate KISS when it comes to MT programming, and that means critical sections for the vast majority.

    ReplyDelete
  5. Lars Fosdal
    , It is used in TThreadedQueue.

    See http://stackoverflow.com/q/4856306/576719 "TThreadedQueue not capable of multiple consumers?" for part of the story.

    ReplyDelete
  6. Fortunately, I don't use that either, Leif Uneus.

    ReplyDelete
  7. Eric Grange I downloaded your source code and played a little bit with it on a dual processor, 4 cores each and hyperthreading. This computer shows 16 CPU in task manager. So I changed the number of threads to 16 and changed samples_bits to 16 to have a longer test. Watching the task manager performance tab, I only see a maximum of 4 active threads. This looks strange. I have not found why yet, but IMO there is something wrong somewhere.

    ReplyDelete

Post a Comment