我看见过一些介绍,记不清了,
线程模型有多种:Single Threaded,Apartment Threaded,
Thread per Object,Thread Pool,Free Thread,...(还有记不清了)
不同的应用可以选择合适的线程模型,不好的选择会影响性能。
多用户下,最好不要用Single Threaded。

解决方案 »

  1.   

    线程+COM对象=单元
    多看看COM+中新提出的中性(线程无关)单元的信息,或许那才是COM上将来解决并发问题之道。
      

  2.   

    Threading Model
    Win32 programming involves understanding processes and threads. A process is an instance of an application that has an address space with four gigabytes of virtual memory and other resources. Threads are objects that live in a process, and are the actual pieces of code that are executed on the processor. Each process starts with one thread, called the primary thread. A process always has one or more threads executing; the process ends when there are no more threads executing. The term multithreading comes from the fact that a process can execute more than one thread.Apartment ThreadedIn an ActiveX DLL project, the Threading Model list box allows you to choose whether your component is single-threaded or apartment-threaded. The default threading model is set to Apartment Threaded.The apartment model is a COM-specific form of multithreading in Windows 95, Windows 98, and Windows NT. An apartment is essentially a way of describing a thread with a message queue that supports COM objects. Visual Basic greatly simplifies the job of authoring in-process components that multithreaded clients can use safely and efficiently. All COM components created with Visual Basic use apartment model threading, whether they're single-threaded or multithreaded.Single ThreadedA single-threaded component has only one apartment, which contains all the objects the component provides. This means that a single-threaded DLL that you create with Visual Basic is safe to use with a multithreaded client. However, there is a performance trade-off for this safety: Visual Basic marshals calls from all client threads except one, just as if they were out-of-process calls.Advantages of Apartment-Model ThreadingMarking an ActiveX DLL project for apartment-model threading provides the following benefits: 
    All of the objects a client creates on a given thread are created in the same apartment (thread) in the DLL. Calls to these objects do not require cross-thread marshaling, making them more efficient.
    When a component has one thread of execution, code for only one object can execute at any given time. COM deals with this situation by serializing requests; the requests are queued and processed one at a time until they are all completed. Because an object is accessed only on the thread where it was created in apartment-model threading, calls are synchronized (serialized) so that a call is never interrupted by a call from another thread.
    Arguments for cross-thread calls are marshaled, and the calling thread is blocked. This synchronization of data protects the calling thread's state.ActiveX EXE ThreadingFor ActiveX Exe projects, you can either specify that each new object be created on a new thread (Thread Per Object), or limit your component to a fixed pool of threads. A thread pool size of one makes the project single-threaded; a larger thread pool makes the project apartment-threaded. Visual Basic provides the following three models for assigning objects to threads in out-of-process components:
    One thread of executionTo use this option, select the Thread Pool option with one thread.
    Thread pool with round-robin thread assignmentTo use this option, select the Thread Pool option and specify the number of threads to allow.
    Every externally created object on its own threadTo use this option, select the Thread Per Object option.A new ActiveX EXE project is single-threaded by default, which means that it can run only one method call at a time. When multiple clients are connected to the server component, this can cause one client's call to a method to block calls from all other clients. To increase the responsiveness of the server component, you can change the threading model. If you choose the Thread Pool option (round-robin thread assignment), you cannot predict when objects will share global data, or when they will block each other from executing. The behavior of the round-robin thread pool algorithm is therefore referred to as nondeterministic. However, the advantage of this threading model is that it puts a limit on the total number of threads.In the Thread Per Object model, as clients request objects, Visual Basic creates each object on a new thread. When the last client releases its last reference to objects on that thread, Visual Basic terminates the thread. The big drawback to the Thread Per Object model is that you have no control over the number of objects (and hence threads) that clients create. Too many active threads-threads that are actively executing code-will bog down the operating system. As a general rule, you want about the same number of active threads as you have processors.
      

  3.   

    COM能够通过单元来提供自动同步,每一个对象都必须在某个特定的单元创建。现在共有三种单元类型STA,MTA,TNA。它们分别对应单线程,多线程,中立线程。不同种类线程访问不同种类的单元里的对象。