MFC Grid manual

CThread Class Reference

Creates and controls a thread. More...

#include <Dapfor/Common/Thread.h>

Inheritance diagram for CThread:

CAutomat

List of all members.


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.

Detailed Description

Creates and controls a 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;
}

Member Enumeration Documentation

anonymous enum

Used types.

Enumerator:
Infinite  Infinite interval.


Constructor & Destructor Documentation

CThread ( const TString &  threadName = TString()  )  [protected]

Constructor.

Parameters:
[in] threadName Name of a thread


Member Function Documentation

void Wait ( int  interval = Infinite  )  [virtual]

Waits for a thread to exit.

Parameters:
[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.

Returns:
The name of the thread.

unsigned int GetThreadId (  )  const

Returns a thread identifier.

Returns:
A thread identifier is returned which is unique within the process

bool IsActive (  )  const [virtual]

Indicates whether a thread is active.

Returns:
True if the thread is active. Otherwise false

bool IsStopping (  )  const [virtual]

Indicates whether the thread is asked to stop but hasn't stopped yet.

Returns:
True if the thread is stopping. Otherwise false