The //demo.h file is as follows:
#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;};
The //demo.cpp file is as follows:
#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 threadHANDLE hthread = CreateThread (NULL, 0, Loopcallbackthread, (void*) This, 0, &dwtid);if (hthread! = NULL){ CloseHandle (Hthread);}}void Cdemo::startthread () {DWORD Dwtid;//Create threadHANDLE hthread = CreateThread (NULL, 0, Loopthread, (void*) This, 0, &dwtid);if (hthread! = NULL){ CloseHandle (Hthread);}}
The DemoData.h file is as follows:
#pragma onceclass 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;};
The DemoData.cpp file is as follows:
#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 files are as follows:
RunningCyclely.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include "DemoData.h" #inc Lude "Demo.h" #include <time.h>int _tmain (int argc, _tchar* argv[]) {Cdemodata Odemodata;Odemodata.m_iinitializevalue = 0;//Initialize ValeOdemodata.m_iaddupnum = 1;//Add up numberOdemodata.m_icallbackcount = 6;//Call back count or call CountOdemodata.m_dwloopms = 1000;//Ms/times for One loopCDemo Odemo (&odemodata);Odemodata.setcallback (&odemo);Odemo.startcallbackthread ();//Start call back thread//odemo.startthread ();//Start common threadOdemodata.startoutputthread ();//Start output threadSystem ("pause");return 0;}
Operation Result:
Please press any key to continue ...
Call Back count:0
Call Back Count:1
Call Back Count:2
Call Back Count:3
Call Back Count:4
Call Back Count:5
Simple application of multithreading