码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象与基于对象 学习记录 thread举例

时间:2015-03-29 13:29:27      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

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

  待续

面向对象与基于对象 学习记录 thread举例

标签:

原文地址:http://www.cnblogs.com/itdef/p/4375529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!