⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cworkerthreadhandler.cpp

📁 window下的多线程编程参考书。值得一读
💻 CPP
字号:
//
// FILE: CWorkerThreadHandler.cpp
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////

#include "CWorkerThreadHandler.h"
#include <stdio.h>

// CWorderThreadHandler implementation.
//
CWorkerThreadHandler::CWorkerThreadHandler( CMclEvent& ExitEvent, BOOL fFault )
    : m_ExitEvent(ExitEvent),
      m_fFault(fFault)
{
}

unsigned CWorkerThreadHandler::ThreadHandlerProc( void )
{
    srand(GetCurrentThreadId());

    printf(
        "[0x%08lx] Worker thread is starting up (%s fault).\n",
        GetCurrentThreadId(),
        (m_fFault ? "will" : "will not")
    );

    int  iWorkCount = 0;
    BOOL fKeepWorking = TRUE;

    while( fKeepWorking )
    {
        // Do some work.
        //
        printf(
            "[0x%08lx] Worker thread is doing work.\n",
            GetCurrentThreadId()
        );

        iWorkCount++;

        // Sleep for one second by waiting for the exit event
        // to be signaled.  If we time out, we'll keep working.
        // If we don't time out, we'll cleanup and exit.
        //
        DWORD dwWaitResult = m_ExitEvent.Wait(1000);

        fKeepWorking = CMclWaitTimeout(dwWaitResult);

        if( fKeepWorking )
        {
            // Check to see if this thread should fault.
            //
            if( m_fFault && (iWorkCount == 4) )
            {
                printf(
                    "[0x%08lx] Worker thread is about to fault.\n",
                    GetCurrentThreadId()
                );

                *((PBYTE)0) = '!';
            }
        }
    }

    printf(
        "[0x%08lx] Worker thread is exiting.\n",
        GetCurrentThreadId()
    );

    return(0);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -