Have you noticed the re-entry problem of "sta single-thread suite" in COM programming?

Source: Internet
Author: User

In COM programming, the choice of COM thread model cannot be avoided. There have been a lot of articles about the "com thread model" on the Internet. Here I just want to say that the "sta single-thread suite" is not as simple as imagined, in actual development, the "re-entry problem" occurs ".

"Sta single-threaded suite" means that a thread can only create one suite, and the COM object created in the thread only belongs to this suite, other threads must call the COM object in this thread in a serial way through the proxy object. In this way, the "sta single-threaded suite" easily solves the "synchronization problem" between multiple threads ".

As a result, we use the "sta single-thread suite" for all COM objects. Is it all right? Of course not. To avoid deadlocks, all com suite types support re-entry. Of course, the "sta single-threaded suite" is no exception. When a thread in the suite calls an object in another suite through a proxy, the caller thread can continue to process other incoming method requests while waiting for the call to complete.

 

For example, object A in thread 1 accesses Object C in thread 3 and Object C accesses object d in thread 4. In this case, thread 4 is processing incoming calls, thread 3 is waiting for thread 4 to return, and thread 1 is waiting for thread 3 to return. Because the "sta single-threaded suite" supports re-entry, when object B in thread 2 initiates access to object C in thread 3, it will be executed immediately. Therefore, Object C in thread 3 is facing the "re-entry problem" test.

In a multi-threaded program, this "re-entry problem" may occur everywhere. If we ignore this problem, we will encounter many inexplicable problems in the future development, and it is extremely difficult to correct them. Because our logic is correct, but the occurrence of "re-entry problem" will change or destroy the code execution environment (variables or loop structures, make the program unable to proceed, or produce incorrect results.

Imagine that every time we implement a COM Object method, we should evaluate whether this method produces a "re-entry problem". If a "re-entry problem" occurs ", whether the execution environment (variable or loop structure) in this method is changed or damaged, and whether the code in other places needs to be adjusted. In this case, the "re-entry problem" is much easier than the "synchronization problem.

 

In short, remember this principle: "MTA multi-threaded suite" considers "synchronization" and "sta single-threaded suite" considers "re-entry ".

 

For more in-depth discussions, refer

COM (simplified) Chapter V suite 5.3 cross-Suite access to p193

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.