• 多线程的简单应用


     //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<<" call 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

  • 相关阅读:
    DDD CQRS架构和传统架构的优缺点比较
    ENode框架单台机器在处理Command时的设计思路
    C#分布式消息队列 EQueue 2.0 发布啦
    EQueue 2.0 性能测试报告
    EQueue文件持久化消息关键点设计思路
    EQueue性能测试计划
    ENode简介与各种教学视频资源汇总(要进群这篇文章必看)
    ENode框架Conference案例分析系列之
    ENode框架Conference案例分析系列之
    ENode框架Conference案例分析系列之
  • 原文地址:https://www.cnblogs.com/freemindblog/p/5992295.html
Copyright © 2020-2023  润新知