Blocking method of an STA COM object is a design defect?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 05:01:11

问题


Say a COM object is created on an STA thread. So all calls to this object are serialized in this thread. So if a method of the object's is blocking, all threads that use this object are blocked.

So having a blocking method in an STA COM object is a design defect to be avoided?

If the COM object is free threading, it is OK to have a blocking method?

Thanks


回答1:


Yes, objects on single-threaded apartments are synchronized via messages and all calls to them are serialized in such way that no more than one method can be called on any such object at any moment of time (also no method can be called more than once at any moment of time). This is by design and is done to achieve a certain degree of thread-safety. Having a long running method by itself is not a big problem unless you introduce a deadlock. Yes, the callers will wait until their calls are run in turn.

Objects in the multi-threaded apartment are not synchronized via messages - all calls to such objects are done directly without synchronization so more than one or more methods can be called at any object at any moment of time in parallel and it's up to the object to ensure thread-safety. Methods of course can block inside, but care should be taken to not introduce a deadlock.



来源:https://stackoverflow.com/questions/6260493/blocking-method-of-an-sta-com-object-is-a-design-defect

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