MFC Grid manual

How to use CThread class?

//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;
}