Ace Active Object Mode (1)

Source: Internet
Author: User

Reproduced in: http://www.cnblogs.com/TianFang/archive/2006/12/11/589168.html

The active object pattern is used to reduce the coupling between method execution and method invocation. This model describes another more transparent method of inter-mission communication.

Traditionally, all objects are passive code snippets, and the code in the object is executed in the thread that issued the method call to it, and when the method is called, the calling thread blocks until the call ends. But the active object is different. These objects have their own command execution threads, and the method of the active object executes in its own execution thread and does not block the calling method.

For example, imagine that the object "A" is already instantiated in the main () function of your program. When your program starts, the OS creates a thread to start execution from the main () function. If you call any method of object A, the thread will "flow" through that method and execute the code in it. Once execution is complete, the thread returns the point at which the method was called and continues its execution. However, if "a" is an active object, it is not the case. In this case, the main thread is not borrowed by the active object. Conversely, when a method of "a" is called, the execution of the method occurs in the thread held by the active object. Another way of thinking: if you call a method of a passive object (a regular object), the call is blocked (synchronous), and on the other hand, if the method of the active object is called, the call is not blocked (asynchronous).

Because the method calls of the active object are not blocked, this improves the system response speed and is useful in network programming.

Here we take a "Logger" (Logger) Object object as an example to describe how to transform a traditional object into an active object, thus improving the system response speed.

Logger's function is to record some system events in memory for querying, because logger uses a slow I/O system to record messages sent to it, so the operation on logger will cause the system to wait for a long time.

The function code is simplified as follows:

1 classLogger: PublicAce_task<ace_mt_synch>2 {3  Public:4     voidLogmsg (Const string&msg)5     {6cout<<endl<<msg<<Endl;7Ace_os::sleep (2);8     }9};

In order to implement the active execution of logging operations, we need to encapsulate them in a command pattern, so that the method of logging can be executed at the right time and place, encapsulated in the following ways:

1 classLogmsgcmd: PublicAce_method_object2 {3  Public:4Logmsgcmd (Logger *plog,Const string&msg)5     {6          This->log=Plog;7          This->msg=msg;8     }9 Ten     intCall () One     { A          This->log->logmsg (msg); -         return 0; -     } the  - Private: -Logger *log; -     stringmsg; + }; -  + classLogger: PublicAce_task<ace_mt_synch> A { at  Public: -     voidLogmsg (Const string&msg) -     { -cout<<endl<<msg<<Endl; -Ace_os::sleep (2); -     } in  -Logmsgcmd *logmsgactive (Const string&msg) to     { +         NewLogmsgcmd ( This, msg); -     } the};

Here is a brief description of the code function:

Ace_method_object is an excuse for the command pattern provided by the ACE, and the command interface call function is an int called call (), where it is possible to encapsulate the invocation of each operation log as a Logmsgcmd object, so that When the original need to call the Logmsg method, as long as the call logmsgactive can generate a Logmsgcmd object, because the Logmsgactive method is called, only the command is encapsulated, and no log operation, so the method will immediately return. It then opens a new thread, passing in the Logmsgcmd object as a parameter, executing the call method of the Logmsgcmd object in that thread, enabling a nonblocking invocation.

However, each time a new thread is opened for a logmsg call, it is undoubtedly a waste of resources, and in fact we tend to insert the generated Logmsgcmd object into a command queue and execute all the commands in the command queue in turn with a new command execution thread. Also, in order to encapsulate the object, the command queue and the command execution thread are often encapsulated in the Logger object, as shown in the following code:

1#include"Ace/os.h"2#include"ace/task.h"3#include"ace/method_object.h"4#include"ace/activation_queue.h"5#include"Ace/auto_ptr.h"6 7#include <string>8#include <iostream>9 using namespacestd;Ten  One classLogger: PublicAce_task<ace_mt_synch> A { -  Public: - Logger () the     { -          This-activate (); -     } -  +     intSvc (); -     voidLogmsg (Const string&msg); +     voidLogmsgactive (Const string&msg); A  at Private: -Ace_activation_queue Cmdqueue;//Command Queue - }; -  - classLogmsgcmd: PublicAce_method_object - { in  Public: -Logmsgcmd (Logger *plog,Const string&msg) to     { +          This->log=Plog; -          This->msg=msg; the     } *  $     intCall ()Panax Notoginseng     { -          This->log->logmsg (msg); the         return 0; +     } A  the Private: +Logger *log; -     stringmsg; $ }; $  - voidLogger::logmsg (Const string&msg) - { thecout<<endl<<msg<<Endl; -Ace_os::sleep (2);Wuyi } the  - //Logging in an active way Wu voidLogger::logmsgactive (Const string&msg) - { About     //Build Command object, insert INTO command queue $Cmdqueue.enqueue (NewLogmsgcmd ( This, msg)); - } -  - intlogger::svc () A { +      while(true) the     { -         //Traverse command Queue, execute command $Auto_ptr<ace_method_object>Mo the( This-cmdqueue.dequeue ()); the  the         if(Mo->call () = =-1) the              Break; -     } in     return 0; the } the  About intMain (intARGC, Ace_tchar *argv[]) the { the Logger log; theLog. Logmsgactive ("Hello"); +  -Ace_os::sleep (1); theLog. Logmsgactive ("ABCD");Bayi  the      while(true) theAce_os::sleep (1); -  -     return 0; the}

Here you need to pay attention to the command queue Ace_activation_queue object, it is thread-safe, the use of the method is relatively simple, here I do not introduce more.

This is the basic structure of the active object, however, since the active object is called asynchronously, the following two new questions are raised:

    1. How does the method invocation thread know that the method has finished executing?
    2. How do I get the return value of a method?

These two problems will be solved in the next.

Ace Active Object Mode (1)

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.