ArcGIS Engine C ++ multithreading [sta MTA com multithreading model]

Source: Internet
Author: User

ArcGIS Engine 10 for crossplatform C ++ProgramWith multithreading, it is okay to use ao in a thread.

However, if the AO license is used for initialization in one threadCodeThe thread that is enabled later) directly use ao. In this way, an error or com error is reported directly.

So I searched for: ArcGIS Engine C ++ multithreading. Although there is no hope, there is little information about arcengine.

The result shows the followingArticleThe content of the two articles is the same. The content is reprinted before, but I will see it first.

Http://blog.sina.com.cn/s/blog_5f08173901010brv.html

Http://anshien.blog.163.com/blog/static/169966308201082441114173/

Where:

2All ArcObjects components of the ArcObjects thread model are markedSingle thread Unit(StaSee the vs help documentation ). Each sta is limited to one thread, but COM does not limit the number of STAS in each process. When a method call enters a STA, it is transferred to the unique thread of the STA. Therefore, an object in the STA will receive and process only one method call at a time, and each method call it receives will reach the same thread. The ArcObjects Component is thread-safe and can be used by developers in a multi-threaded environment. For the effective running of AO applications in a multi-threaded environment, the thread unit model used by AO, that is, independent threads, must be considered. This model works to eliminate cross-thread communication. Reference of all ArcObjects objects in a thread should only communicate with objects in the same thread. For the running of this model9In. X, a single object is designed to be unique in a thread rather than a process. The resource consumption for managing multiple objects in the process exceeds the performance improvement obtained by stopping cross-thread communication. For developers who extend the ArcGIS system, all objects, and even the objects you create, must follow this rule to work in isolated threads. If you create objects as part of development, you must ensure that they are unique in the thread, rather than unique in the process. The only thread is to prevent cross-thread communication. Here is the primary rule of multithreading in ArcGIS Engine.

This means that all Ao items should be put in one thread to work, and ao com components cannot be used across threads.

Okay, I'm speechless, and there's no solution to this problem .. I really don't understand why it is limited to a thread.

As in the last article, leave a message:

Well written. I recently encountered the problem of multithreading in the Development of AE, which has not been solved. Finally, give up and return to the single-thread development. It is still very troublesome to use COM objects and multithreading together. After reading your article, it is very rewarding.

AE and multithreading are troublesome. Finally, it is still the error of the COM object.

I don't know if I understand it wrong ..

----------------

This requires that AE can be used in multiple threads. It can also be used to make minor modifications to the thread and set the thread attribute to sta.

There are two thread models: STA and MTA.

Sta (single threaded apartments ). Apartment is just a logical concept. It can contain one or more threads. An appdomain can contain one or more apartments. Sta means that the apartment can only contain one thread.
MTA (multi threaded apartments ). This apartment can contain multiple threads.
The biggest difference between STA and MTA is that MTA can use all the shared resources in the same apartment to execute multiple threads concurrently. While multiple Stas can share data, but cannot execute threads concurrently, there is a performance problem.

Specific Search: sta mta.

Single- Threaded apartmentsa process has one single -Threaded apartment (STA) For Each thread that called coinitialize. Each of these apartments, In  Turn, may have zero or more COM objects associated with them. as the name implies, however, only one specific thread (the thread that created the apartment by calling coinitialize) may directly access the objects within the apartment. once associated with an apartment, a thread cannot be associated with another apartment or change the concurrency model of its apartment. to create an STA, a thread simply callcoinitialize or coinitializeex (null, coinit_apartmentthreaded), thereby associating the calling thread with the apartment until it callcouninitialize. successive callto either of these functions by the same thread  Do Not create additional apartments or enable you to change the concurrency model of the apartment. multi - Threaded apartmentsalthough multi -Threaded apartments, sometimes called free-threaded apartments, are a much simpler model, they are more difficult to develop For Because the developer must implement the thread synchronization For The objects, a decidedly nontrivial task. On the positive side, removing an sta ' S synchronization mechanic gives the developer much finer control over the use of thread synchronization. they can apply it where it is actually needed rather than taking the very conservative approach of Stas, which synchronize access to the entire apartment.  The first thread  In A process to call coinitializeex (null, coinit_multithreaded) creates a multi-threaded apartment that will be shared with any other threads that repeat This Call. Once a thread Is Associated with the MTA, it can directly access any Object Created therein, which introduces some subtle variations From The way Stas operate. In Stas, a specific thread, permanently associated with the apartment, creates Object And only This Thread can ever access Object ( As Discussed In The preceding section). In an MTA, however, writable threads may create objects In The apartment and no thread controls access. As a result, it Is Possible to have several threads simultaneously access Object And change Class Data; hence the need For Synchronization objects to protect Class  Data. It shocould come  As No surprise that, As With Stas, Global Data Is Not thread-safe In An MTA and always needs to be adequately Protected . Remember to take advantage of the inherently thread-local nature of local and automatic variables: They ' Re allocated on the thread  ' S stack and consequently Do Not need to be Protected  . In -Process components written For MTA use the threadingmodel registry value (threadingmodel = Free) to indicate to clients that they shoshould be created In The MTA. In -Process component marked As Such will be created In The MTA, regardless of the apartment type of the client. if necessary, com will create the MTA before creating the component. coinitializeex (null, coinit_apartmentthreaded) couninitialize () coinitializeex (null, coinit_multithreaded) from: understanding and using COM threading modelshttp:  //  Msdn.microsoft.com/en-us/library/ms809971.aspx HTTP: //  Msdn.microsoft.com/zh-cn/library/ms809971.aspx
Http://social.msdn.microsoft.com/search/en-US? Query = sta
Http://msdn.microsoft.com/en-us/library/windows/desktop/ms680112 (V = vs.85). aspx

 

The coinitlizeex function is added to the thread to specify the Stas attribute.

 

 

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.