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. |
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.
// 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();
CAutomat | ( | const TString & | threadName = TString() |
) |
Constructor.
[in] | threadName | Thread name |
void PushJob | ( | CJob * | job | ) |
Puts a job to the internal queue. The function is not blocking.
[in] | job | Job to be processed. |
void OnExecuteJob | ( | CJob * | job | ) | [protected, virtual] |
Called to execute a job in the automat's thread. After processing, the job is destroyed.
[in] | job | Job to be processed. |
void WaitNextJob | ( | unsigned int | interval | ) | [protected, virtual] |
Frees processor resources by calling WaitForSingleObject function and waits for the next job.
[in] | interval | Interval that indicates how long the thread sleeps in WaitForSingleObject() function. |
int Run | ( | ) | [protected, virtual] |
Main routine.
Implements CThread.
Copyright Dapfor 2007-2009 | Generated on Wed Jul 7 03:24:43 2010 for MFCGrid by 1.5.5 |