MFC Grid manual

CAutomat Class Reference

CAutomat is targeted for asynchronous CJob processing. More...

#include <Dapfor/Common/Automat.h>

Inheritance diagram for CAutomat:

CThread

List of all members.


Public Member Functions

 CAutomat (const TString &threadName=TString())
 Constructor.
virtual ~CAutomat ()
 Virtual destructor.
void PushJob (CJob *job)
 Puts a job to the internal queue. The function is not blocking.
virtual void Start ()
 Starts the automat's thread.
virtual void Stop ()
 Asks the thread to stop.
virtual void Wait (int interval=Infinite)
 Waits for the thread to exit.

Protected Member Functions

virtual void OnExecuteJob (CJob *job)
 Called to execute a job in the automat's thread. After processing, the job is destroyed.
virtual void WaitNextJob (unsigned int interval)
 Frees processor resources by calling WaitForSingleObject function and waits for the next job.
virtual int Run ()
 Main routine.

Detailed Description

CAutomat is targeted for asynchronous CJob processing.

CAutomat is targeted for asynchronous processing of jobs, presented in the form of CJob class. The automat can receive jobs from any thread and process them in its own thread. Implementation of this class enables optimal use of processor resources, and if there are no jobs, the automat virtually doesn't consume processor resources at all. To put a job in the queue CAutomat::PushJob() function is called. While receiving a job, the automat wakes up, puts job to the queue and calls CJob::ProcessJob method in its own thread. When the job is processed, the automat deletes the object of CJob class. If there are no more jobs in the queue, the automat "falls asleep" again. The main difference between CAutomat and CThread classes is that the automat does not consume processor resources, when it does not process a job.

Thread safety.
All functions of CAutomat class are thread safe.
// MyAutomat.h file
class CMyAutomat : public Dapfor::Common::CAutomat
{
public:
    CMyAutomat(const std::string& name);
};

// MyAutomat.cpp file
CMyAutomat::CMyAutomat(const std::string& name) : CAutomat(name)
{
}



// PrintJob.h file

//The job that will be processed in the automat's thread.
class CPrintJob : public Dapfor::Common::CJob
{
public:
    CPrintJob(int count);

protected:
    virtual void ProcessJob(Dapfor::Common::CAutomat* automat);

private:
    int m_Count;
};



// PrintJob.cpp file
CPrintJob::CPrintJob(int count) : m_Count(count)
{
}

void CPrintJob::ProcessJob(Dapfor::Common::CAutomat* automat)
{
    //Do some work...
    for(int i = 0; i < m_Count; ++i)
    {
        std::cout << automat->GetThreadName() << ": value = " << i << std::endl;
        Sleep(100);
    }
}



//Using sample

  ...

    //Create the automat
    CMyAutomat automat("Automat");

    //Start the automat
    automat.Start();

    //Create and push a new job and push it to the automat.
    //Job object will be destroyed upon processing.
    automat.PushJob(new CPrintJob(10));

    //Wait for job processed in the automat
    Sleep(2000);

    //Stop the automat
    automat.Stop();
    automat.Wait();

Constructor & Destructor Documentation

CAutomat ( const TString &  threadName = TString()  ) 

Constructor.

Parameters:
[in] threadName Thread name


Member Function Documentation

void PushJob ( CJob job  ) 

Puts a job to the internal queue. The function is not blocking.

Parameters:
[in] job Job to be processed.
Thread safety
The function is thread safe

void OnExecuteJob ( CJob job  )  [protected, virtual]

Called to execute a job in the automat's thread. After processing, the job is destroyed.

Parameters:
[in] job Job to be processed.
Thread safety
The function is thread safe.

void WaitNextJob ( unsigned int  interval  )  [protected, virtual]

Frees processor resources by calling WaitForSingleObject function and waits for the next job.

Parameters:
[in] interval Interval that indicates how long the thread sleeps in WaitForSingleObject() function.
Thread safety
The function is thread safe.

int Run (  )  [protected, virtual]

Main routine.

Returns:
Zero if the thread is correctly completed. Otherwise a value, different from zero.

Implements CThread.