Public Types | |
enum | { Infinite = 0xFFFFFFFF } |
Used types. More... | |
Public Member Functions | |
virtual void | Start () |
Starts a new thread. | |
virtual void | Stop () |
Asks a thread to stop. | |
virtual void | Wait (int interval=Infinite) |
Waits for a thread to exit. | |
virtual TString | GetThreadName () const |
Returns the name of the thread, specified in the constructor. | |
unsigned int | GetThreadId () const |
Returns a thread identifier. | |
virtual bool | IsActive () const |
Indicates whether a thread is active. | |
virtual bool | IsStopping () const |
Indicates whether the thread is asked to stop but hasn't stopped yet. | |
Protected Member Functions | |
CThread (const TString &threadName=TString()) | |
Constructor. | |
virtual | ~CThread () |
Virtual destructor. Releases all the resources used by CThread object. | |
virtual int | Run ()=0 |
This function is called in a new thread. |
//Basic example of using threads // MyThread.h file #pragma once #include <Common/Thread.h> using namespace Dapfor; class CMyThread : public Dapfor::Common::CThread { public: CMyThread(const std::string& name); protected: virtual int Run(); }; // MyThread.cpp file CMyThread::CMyThread(const std::string& name) : CThread(name) { } //The function is called when a new thread is created int CMyThread::Run() { //Do something... for(int i = 0; i < 10; ++i) { std::cout << GetThreadName() << ": value = " << i << std::endl; Sleep(100); } return 0; } //Using sample ... typedef std::vector<CMyThread*> Threads; Threads threads; //Create 10 threads for(int i = 0; i < 10; ++i) { std::string name("Thread "); name += Dapfor::Common::CLongFormat().Format(i, 0); CMyThread* thread = new CMyThread(std::string(name)); threads.push_back(thread); thread->Start(); } //Wait for 5 seconds Sleep(5000); //Stop all threads and free all resources. for(Threads::iterator it = threads.begin(); it != threads.end(); ++it) { (*it)->Stop(); (*it)->Wait(); delete *it; }
CThread | ( | const TString & | threadName = TString() |
) | [protected] |
Constructor.
[in] | threadName | Name of a thread |
void Wait | ( | int | interval = Infinite |
) | [virtual] |
Waits for a thread to exit.
[in] | interval | Integer specifying time interval in milliseconds to wait for the thread to terminate. May be used CThread::Infinite value |
Reimplemented in CAutomat.
TString GetThreadName | ( | ) | const [virtual] |
Returns the name of the thread, specified in the constructor.
unsigned int GetThreadId | ( | ) | const |
Returns a thread identifier.
bool IsActive | ( | ) | const [virtual] |
Indicates whether a thread is active.
bool IsStopping | ( | ) | const [virtual] |
Indicates whether the thread is asked to stop but hasn't stopped yet.
Copyright Dapfor 2007-2009 | Generated on Wed Jul 7 03:24:43 2010 for MFCGrid by 1.5.5 |