首页 > 代码库 > 多线程的简单应用
多线程的简单应用
//Demo.h文件如下:
#pragma once
class CDemoData;
typedef void (CALLBACK* pCallBack)(void* pParam);
class CDemo
{
public:
CDemo(void);
CDemo(CDemoData* pData);
~CDemo(void);
void CyclelyCallBack(LPVOID* pParam);
void StartCallBackThread();
void StartThread();
CDemoData* m_pData;
pCallBack m_pCallBack;
};
//Demo.cpp文件如下:
#include "StdAfx.h"
#include "Demo.h"
#include "DemoData.h"
DWORD WINAPI LoopCallBackThread(void* pParam)
{
CDemo* pDemo = (CDemo*)pParam;
int i = 0;
do
{
Sleep(pDemo->m_pData->m_dwLoopms);
pDemo->m_pCallBack(pParam); // call back CallBackCount()
++i;
} while (i < pDemo->m_pData->m_iCallBackCount);
return 0;
}
DWORD WINAPI LoopThread(void* pParam)
{
CDemo* pDemo = (CDemo*)pParam;
int i = 0;
do
{
Sleep(pDemo->m_pData->m_dwLoopms);
pDemo->m_pData->AddCount(); // call CDemoData AddCount()
++i;
} while (i < pDemo->m_pData->m_iCallBackCount);
return 0;
}
CDemo::CDemo(void)
{
}
CDemo::CDemo(CDemoData* pData)
{
m_pData = http://www.mamicode.com/pData;
}
CDemo::~CDemo(void)
{
}
void CDemo::StartCallBackThread()
{
DWORD dwTID;
// create thread
HANDLE hThread = CreateThread(NULL, 0, LoopCallBackThread, (void*)this, 0, &dwTID);
if(hThread != NULL)
{
CloseHandle(hThread);
}
}
void CDemo::StartThread()
{
DWORD dwTID;
// create thread
HANDLE hThread = CreateThread(NULL, 0, LoopThread, (void*)this, 0, &dwTID);
if(hThread != NULL)
{
CloseHandle(hThread);
}
}
//DemoData.h文件如下:
#pragma once
class CDemo;
class CDemoData
{
public:
CDemoData(void);
~CDemoData(void);
void SetCallBack(CDemo* pDemo);
void AddCount();
void StartOutputThread();
int m_iInitializeValue;
int m_iAddUpNum;
int m_iCallBackCount;
DWORD m_dwLoopms;
};
//DemoData.cpp文件如下:
#include "StdAfx.h"
#include "DemoData.h"
#include "Demo.h"
void WINAPI CallBackCount(void* pParam)
{
CDemo* pDemo = (CDemo*)pParam;
pDemo->m_pData->m_iInitializeValue += pDemo->m_pData->m_iAddUpNum;
}
DWORD WINAPI ToOutputThread(void* pParam)
{
CDemoData* pData = http://www.mamicode.com/(CDemoData*)pParam;
int i = 0;
do
{
Sleep(pData->m_dwLoopms);
cout<<"\ncall back count:"<<pData->m_iInitializeValue; // output initialize value
++i;
} while (i < pData->m_iCallBackCount);
return 0;
}
CDemoData::CDemoData(void)
{
m_iInitializeValue = http://www.mamicode.com/0;
m_iAddUpNum = 0;
m_iCallBackCount = 0;
m_dwLoopms = 1000;
}
CDemoData::~CDemoData(void)
{
}
void CDemoData::SetCallBack(CDemo* pDemo)
{
pDemo->m_pCallBack = CallBackCount;
}
void CDemoData::AddCount()
{
m_iInitializeValue += m_iAddUpNum;
}
void CDemoData::StartOutputThread()
{
DWORD dwTID;
HANDLE hThread = CreateThread(NULL, 0, ToOutputThread, (LPVOID)this, 0, &dwTID);
if(hThread != NULL)
{
CloseHandle(hThread);
}
}
// Main.cpp文件如下:
// RunningCyclely.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DemoData.h"
#include "Demo.h"
#include <time.h>
int _tmain(int argc, _TCHAR* argv[])
{
CDemoData oDemoData;
oDemoData.m_iInitializeValue = http://www.mamicode.com/0; // initialize vale
oDemoData.m_iAddUpNum = 1; // add up number
oDemoData.m_iCallBackCount = 6; // call back count or call count
oDemoData.m_dwLoopms = 1000; // 1000 ms/times for one loop to call back
CDemo oDemo(&oDemoData);
oDemoData.SetCallBack(&oDemo);
oDemo.StartCallBackThread(); // start call back thread
//oDemo.StartThread(); // start common thread
oDemoData.StartOutputThread(); // start output thread
system("pause");
return 0;
}
//运行结果:
请按任间键继续...
call back count:0
call back count:1
call back count:2
call back count:3
call back count:4
call back count:5
多线程的简单应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。