ActiveX .exe .dll Server multi-thread
Source: CWW / AKL
Yuqun translated into Simplified
ActiveX .exe .dll Server can be set to multi-thread, but most of the manual is not seen.
Understand, especially Project itself can set multi-thread, and Class can also set into multiuse, not
It is very clear that after some Testing, some things are sorted out, so that the manual is unclear, after reading it
Going to see the manual will be clear (I think). If my idea is incorrect, please correct it.
MyProject assumes that there is an ActiveX Server with a Class and .rable Module
|
- Class
| | |
| --- Myclass
Public CP As Long
Private CV As Long
Public Sub CFUNC1 ()
.
.
| END FUNCTION
Public Sub CFUNC2 ()
.
.
| END SUB
|
- Module
|
- mybas.bas
Public Basvar As Long
Public Sub Sub1 ()
.
.
End Sub
First, MyProject is ActiveX .exe Server
1.myProject is set to a single thread
A.myclass is Multiuse
Each instance that represents the Class is fixed by a THREAD.
The so-called MultiUse is a single thread to serve multiple instances.
Client a program client B program
Dim Aa As New Myclass Dim BB AS New Myclass
Call aa.cfunc1 call bb.cfunc2
As in the above example, Clienta Clientb also made a need for MyProject on a computer.
At this point, the first call has a program (assuming that Call aa.cfunc1) calls the server,
If ClientB is also requested, then CLINETA AA this instance and
Client B's BB Instance shared with THREAD, that is, these two instance must
Do you need to enter the schedule, what characteristics do you have a THREAD? AA, BB shared at this time
Information within Mybas.bas, such as Basvar in the above example, shared, whether it is changed it will affect
Another person, but BB, AA these two class instance have their own variables, that is, AA has yourself
CP, CV variables, BB also has its own, this is the so-called apartment mode, in the .CLX
Everyone has some people, but share .rable information. In this example, if aa.cfunc1 is already
Implement, and it is a long job, and bb.cfunc2 is also executed, it has to enter
Queue is waiting, so there will be traffic blocked.
B.singleuse
Indicates that a Class's instance is only executed on a Thread, and the AA this instance is generated by the example above, then it is executed on Thread1, and the BB is also generated, then
It will execute on another Thread2, and Thread1, Thread2 itself has its own area information.
Therefore, AA, BB will not use any information in Mybas.bas, because of AA, BB in different Thread
Upon executing, so two of them are executed in the way, no one will do, another person must
Get, the aa's Thread will do it first, and it will be done after a while, it is possible to do SWAP BB.
Bb.cfunc2 is completed first. But the disadvantage is to waste resource. Don't be given by MyProject's single thread
Deceived, it is set to a single thread, but when Class is set to Singleuse, it is still possible to
Multithread's Server
another example:
There are two classes in MyProject, class1 is Multiuse, class2 is Singleuse
And there is a Mybas.Bas Module
DIM AA As New Class1
DIM BB As New Class1
DIM AA2 As New Class2
DIM BB2 As New Class2
Aa.method1
Aa2.method2
bb.func1
BB2, FUNC2
Then AA, AA2, BB share mybas.bas information, BB2 is not shared, AA, BB is the same thread
No problem, what about AA2? That's because the SingleUse object is Class, which is a AA, BB
On it, but that is Class1, Class2 has not yet instance in Thread, so choose a place.
Thread is executed, and BB2 is naturally on another Thread. This premise is that there is only one client.
To perform, if there are more than two clients, we do not know the pre-rear order of the execution, so it is not easy to predict
Who is sharing information. And we can get the ID of Thread through app.threadid, in the above example
If there is a reference app.threadID in Class1, in Class2, it will be found that AA, AA2, BB have achieved
The same is the same, while the BB2 is not the same.
2. MYPROJECT is set to multi-threaded
Suppose we set MYPROJECT with 4 Thread (T1 to T4)
T1
|
|
T4 - ---- T2
|
|
T3 If there is a client1 to 3 call Server's order
Classa (MULTIUSE) Client1 requires ClassA -> A1 to execute in T1
Classb (MultiUse) Client2 requires Classa -> A2 .. T2 ..
Classc (multiuse) client1 .. Classb -> b1 .. T3 ..
Mybas.bas client3 .. Classc -> c3 .. T4 ..
Client2 .. Classc -> C2 .. T1 ..
Client1 .. Classc -> C1 .. T2 ..
That is, each of the client ends generates an instance, which will be on the next three
Execution, such a loop, so this example, becomes A1, C2 share mybas.bas, A2, C1 is another group of shared people. This is very unclear that who and shared materials are very unclear, but they have yourself every.CLX has yourself.
Information, in line with apartment models.
In addition, if instance A1 (CLASSA Instance generated by Client1) is performed in T1, and A1
When you go to CREATE INSTANCE B, IF uses the new instruction, INSTANCE B is also executed in T1, so the A1 is shared
Mybas.bas, but, INSTABCE B will be executed in T2 using CreateObject.
As for three classes, one is SingleUse, I have not measured, I can know is
SingleUse's class will find a thread without performing the same class, but if 4 Thread
Sometimes, will not produce the fifth thread to do, maybe, there is no strength Testing.
3. MYPROJECT is set up a standalone thread
With a multi-thread, just limit the number of Thread.
Second, MyProject is ActiveX. DLL Server
Because it is .dll's way, Class cannot be SINGLEUSE because a processs
I can't have the same two copies. DLL
1.myProject set a single thread
Client multi-thread. EXE has Thread 1 -3
Thread1, thread2, thread3 simultaneously asked the Class, then wait
The front processing is over, because the Server only has a single thread. Each Instance
Shared.
2. MYPROJECT is set up an apartment model (multi-thread)
In the same way, Thread1- 3 The operation of the Class is no such thing.
Because of the multi-thread. EXE's THREAD, it defines a question in DLL Server.
All the objects built in the thread live in the apartment defined in this thread unless Thread3
With THREAD2 object method, then this call will be in THREAD2 itself is currently being processed.
After the object (step by step).
The following is the sort of AKL
First, you will be emphasized in the proprietary noun involved in this theme:
Components: Refers to ActiveX Server (or Ole Server) that provides one or several object categories.
Process: Process, the address of the program, the data of the program and the program itself are stored in this location space, which can be considered as the space of the program activity. It is where the object exists, an ActiveX Server (this refers to EXE) executes the individual A process.
Thread: thread, in order to occupy an OS time-time job, the execution of the program requires Thread, if there is a plurality of Thread in a program, you can do multiple work at the same time, and occupy the entire OS. Note: The Chinese translation of MSDN translates the process as a process, while thread translated as threads (domestic C or C comparison books all translated the process as a process, but Thread translated as thread)
Six types of object categories
Private
Only the inside of the component can be used, which is invisible.
PublicnotCreatable
The Client end can be referenced (visible), but it is not possible to use the Callback behavior of the component.
Singleuse
The CLIENT ends require each other object entities that have their own components to perform individuals.
GlobalSingleuse
Many global represents the components to become a domain object, there is no need to reference when used. Multiuse
Each of the same categories of object entities requested by the Client end uses the same component to perform an individual.
GlobalmultiUse
Many global represents the components to become a domain object, there is no need to reference when used.
ActiveX EXE
Relationship between the components obtained by MultiUSE objects.
VB Chinese version option
My own explanation
CLIENT number
PROCESS
Number of THREADs for each process
Independent thread
Independent Thread
N
1
N 1
Thread shared area 1 thread
Single THREAD
N
1
1
Thread shared area M thread
Shared M THREAD
N
1
N 1 (N 1 ≤ M)
Relationship between the components obtained by SINGLEUSE objects.
VB Chinese version option
My own explanation
CLIENT number
PROCESS
Number of THREADs for each process
Independent thread
Independent Thread
N
N
1
Thread shared area 1 thread
Single THREAD
N
N
1
Thread shared area M thread
Shared M THREAD
N
N
1
Note: The client end in this test is a single process.
Thread model (independent / shared n process)
Category Instancing (SingleUse / MultiUse)
significance
The component performs whether the individual is more Thread. The components of the stand-alone thread perform individuals to establish a Thread for each object entity, so a total of (the number of objects 1) a Thread.
An object entity in an individual executing an individual is required to perform an individual by the component to perform the use of the CPU when performing a work. If a component can have multiple Thread, indicating that multiple object entities can work simultaneously without waiting for the object to take the Thread. THREAD. If the thread model of the component is set to share more than one thread, that is, the apartment model is equal.
SINGLE and MULTI refer to how object entities established by the Client End exist in the component execution individual (ie, the components of the component), that is, whether there is an object that can exist in the active individual of the component (SingleUse can not, MultiUse) . If not only one object category is provided in this component, the same component is performed in the case where the SINGLEUSE category is not repeated. Once the SINGLEUSE category is used to establish more than one or more, another component will be automatically generated, and an object of the SingleUse category that generates the call end in the new component is executed in the new component.
influences
Do different objects in the same element can operate together in an individual (Process).
Due to the difference in the space exists (different), the SingleUse object cannot share all domain data between the same category object. Only Multiuse's objects can share all domain information and enjoy a single process, multiple Thread benefits.
Whether it is ActiveX DLL or ActiveX EXE, the key to sharing the PUBLIC variable in the general module is that the object entity exists in the same component to perform an individual (Process). Since the ActiveX DLL is component belonging to in-Process, the object category module does not have SINGLEUSE INSTANCING, and the object entity generated by the other of which is provided is existing in the process of the Client side. If multiple objects are present at the same time in the Client side In Process, the PUBLIC variable in the general module can be shared. In terms of Out-Process's ActiveX EXE, if the object established by the client is SingleUse, no matter what the thread model of the component project is set, it will generate a new component to perform an individual (Process); if the object Category is MultiUse, and the thread model of the component project is a stand-alone thread, and multiple Threads are created in the same component to perform individual THREADs to correspond to the object entities established by each Client end. Conclusion: SINGLEUSE / MULTIUSE of the object category is whether the components are MULTIPROCESS; the component thread model "independent or shared N / shared 1" is the component is Multithread.