Delphi - Is TClientDataset Thread Safe?

梦想的初衷 提交于 2019-11-30 13:12:33

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.

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.

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

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