In the previous section on active objects of ACE, I have briefly introduced the active object mode. Today I will briefly introduce the implementation of active objects in C.
The so-called active object is relative to the passive object. The method call and execution of the passive object are in the same thread, passive object method calls are synchronous and congested. Generally, objects belong to passive objects. The call and execution of active object methods are separated, the active object has its own independent execution thread. The method call of the active object is initiated by other threads, but the method is executed in its own thread, the call of the Active Object method is asynchronous and non-blocking.
The framework consists of the following parts:
- An activeobject class, inherited from thread, encapsulates the activity object of the concurrency Logic
- An activequeue class mainly used to store caller requests
- An imethodrequest interface is mainly used to encapsulate caller requests. It is an implementation method of the command design mode.
A simple implementation is as follows:
InterfaceImethodrequest { VoidCall (); } ClassActivequeue <t> { Const IntMaxcount = 10;//Maximum Queue <t> queue = New Queue <t> (); Public Void Enqueue (T item) { Lock ( This ) { If (Queue. Count> = maxcount) { Monitor. Wait ( This ); } Queue. enqueue (item ); Monitor. pulseall ( This ); } } Public T dequeue () { Lock ( This ) { If (Queue. Count = 0) { Monitor. Wait (This ); } T item = queue. dequeue (); Monitor. pulseall ( This ); Return Item; } } } ClassActiveobject { Activequeue <imethodrequest> queue =NewActivequeue <imethodrequest> (); PublicActiveobject () { Threadpool. queueuserworkitem (excutecommand ); } Public VoidAddcommand (imethodrequest cmd) { Cmdqueue. enqueue (CMD ); } Private VoidExcutecommand (Object OBJ) { While(True) { Imethodrequest cmd = queue. dequeue (); Cmd. Call (); } } } |
In the three active object members activeobject, imethodrequest, and activequeue, the first two are relatively simple and easy to think of implementation methods. A little more complex is the activequeue object, which requires a thread-safe operation class. Here I use a simple encapsulation of the queue like Ace, in order to make it easy to use, I simply modified its enqueue and dequeue methods.
- When the enqueue method is used, if the number of elements in the queue has reached the maximum number limit, the elements in the queue will be removed by dequeue.
- When the enqueue method is used, if no available element exists in the queue, it will block other threads to insert the element into the queue through enqueue, and then return the available element.
I used monitor to implement thread synchronization. If you are interested, refer to it.
Active object is a common asynchronous communication mode. Here I simply provide a C # implementation. Of course, my examples here are not perfect. For more information, refer to the chapters I used to introduce active ace objects.