Simple application of multithreading

Source: Internet
Author: User

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-&GT;M_PDATA-&GT;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-&GT;M_PDATA-&GT;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-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.