标签:
// 1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
class CThread{
public:
CThread();
virtual ~CThread();
bool Start();
void Join();
static DWORD WINAPI ThreadProc( LPVOID lpParameter);
virtual void Run() = 0;
private:
HANDLE hThread_;
DWORD dwThreadId_;
};
CThread::CThread():
hThread_(NULL),dwThreadId_(0)
{
cout << "Thread ..." << endl;
}
CThread::~CThread()
{
if(hThread_ != NULL)
CloseHandle(hThread_);
cout << "~Thread ..." << endl;
}
bool CThread::Start()
{
bool bRet = false;
hThread_ = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc, // thread function
this, // argument to thread function
0, // use default creation flags
&dwThreadId_); // returns the thread identifier
if(hThread_)
{
bRet = true;
}
return bRet;
}
void CThread::Join()
{
WaitForSingleObject(hThread_,3000);
}
DWORD CThread::ThreadProc( LPVOID lpParameter)
{
CThread* thread = static_cast<CThread*>(lpParameter);
thread->Run();
return NULL;
}
class CMyThread:public CThread
{
public:
void Run(){ cout << "my thread..." << endl;}
};
int _tmain(int argc, _TCHAR* argv[])
{
while(1)
{
CMyThread a;
a.Start();
a.Join();
}
return 0;
}
待续
标签:
原文地址:http://www.cnblogs.com/itdef/p/4375529.html