Delphi - Is TClientDataset Thread Safe?

拟墨画扇 提交于 2019-12-18 15:49:10

问题


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?

Edit

The cloned image is used in a read only mode.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/669319/delphi-is-tclientdataset-thread-safe

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