多线程的简单应用

Posted 明月忧忧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程的简单应用相关的知识,希望对你有一定的参考价值。

 //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 = 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 = (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 = 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 = 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

以上是关于多线程的简单应用的主要内容,如果未能解决你的问题,请参考以下文章

一个简单Java多线程的应用

一个简单的JAVA C/S多线程应用

java多线程wait,notify,countDownLatch的一些简单应用

在多线程C ++应用程序中,我是否需要一个互斥锁来保护一个简单的布尔值?

多线程 - CallableFuture 和 FutureTask 简单应用

使用 Qt 的简单多线程:我这样做对吗?