Delphi - Is TClientDataset Thread Safe?

后端 未结 3 611
情歌与酒
情歌与酒 2021-01-02 05:00

I have a TClientDataset that is managed in Thread 1.

In a different thread I have a cloned Image of the TClientDataset.

Will I run into threading problems?

相关标签:
3条回答
  • 2021-01-02 05:17

    The short answer is no, a ClientDataSet is not thread safe. The more involved answer is that it depends on how you use it. No matter how many clones of the ClientDataSet you have, they are safe to use from multiple threads so long as you are only reading the data. This is true even if you are setting different ranges, current records, filters, and so forth.

    And, though you did not ask about this, you can free any of the cloned ClientDataSets, even the original ClientDataSet that was cloned, without problems.

    On the other hand, if there is any need to ever post a record to any of the clones, or reload the data, you must use a synchronization object. If the changes are rare, the TMultiReadExclusiveWriteSynchronizer class is great one to use for this purpose.

    0 讨论(0)
  • 2021-01-02 05:17

    The clone process should be handled with a TCriticalSection, but each TClientDataSet should be fine in its own thread.

    0 讨论(0)
  • 2021-01-02 05:24

    In a word: No.

    All of the VCL should be considered "thread unsafe". Any calls to visual components in a TThread should be made in a Synchronize event.

    Any VCL/RTL class should be created and destroyed entirely within a TThread.

    0 讨论(0)
提交回复
热议问题