Ace thread management mechanism-object-oriented Thread class ace_task

Source: Internet
Author: User

When we use ace_thread for packaging in the previous chapter, you must have noticed some areas that are not "elegant. The majority in that chapterProgramAre decomposed into functions rather than objects. This is because the ace_thread packaging requires a global function name or a static method as the parameter. Then this function (static method) is used as the "Startup point" of the derived thread ". This naturally requires programmers to write a function for each thread. As we can see, this may lead to non-object-oriented program decomposition.

Ace_task Is Oo encapsulated for common thread processing. ace_task can be used to perform better operations on threads.

To create a task, follow these steps:

    1. How to initialize and terminate a service:
      Open ()The method should contain initialization of all dedicated tasks.Code. These may include resources such as connection control blocks, locks, and memory.Close ()Method is the corresponding termination method.
    2. Call the activation method:
      After the active object is instantiated, you must callActivate ()Enable it. The number of threads to be created in the active object, and other parameters are passedActivate ()Method.Activate ()Method will makeSVC ()Method becomes the start point of all the threads it generates.
    3. Specific Service Processing Methods:
      As mentioned above, after the active object is enabled, each new threadSVC ()Method. The application developer must define this method in the subclass.

The following example shows how to create a task:

# Include "ACE/task. H"
# Include "ACE/OS. H"

# Include <Iostream>
Using NamespaceSTD;

class taskthread: Public ace_task
{< br> Public :
virtual int SVC ( void )
{< br> for ( int I = 0; I <10; I ++)
{< br> ace_ OS: Sleep (1);
cout " Hello thread1 " }< br> return 0;
}< BR >};

IntMain (IntArgc,Char* Argv [])
{
Taskthread task;
Task. Activate ();

While(True)
Ace_ OS: Sleep (10 );

Return0;
}

Ace_task also encapsulates common thread operations, such as pause, resume, and stop. Is it very simple and convenient.

In fact, the use of ace_task is more than just this. through it, you can also implement a very common network programming mode-active object mode, its specific functions will be detailed in the subsequent design model section.

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.