STA COM 对象的阻塞方法是设计缺陷吗?
假设 COM 对象是在 STA 线程上创建的。因此对该对象的所有调用都在该线程中序列化。因此,如果对象的某个方法处于阻塞状态,则使用该对象的所有线程都会被阻塞。
那么在 STA COM 对象中拥有阻塞方法是一个需要避免的设计缺陷吗?
如果COM对象是自由线程的,那么有阻塞方法可以吗?
谢谢
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
是的,单线程单元上的对象通过消息进行同步,并且对它们的所有调用都以这样的方式序列化,即在任何时刻都不能在任何此类对象上调用超过一个方法(也不能多次调用任何方法)任何时刻)。这是设计使然,这样做是为了实现一定程度的线程安全。除非引入死锁,否则拥有一个长时间运行的方法本身并不是一个大问题。是的,调用者将等待,直到他们的调用依次运行。
多线程单元中的对象不通过消息同步 - 对此类对象的所有调用都是直接完成的,无需同步,因此可以在任何时间并行调用任何对象上的多个方法,并且由对象决定确保线程安全。方法当然可以在内部阻塞,但应注意不要引入死锁。
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.