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

📄 cmadthreadhandler.cpp

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

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

CMclEvent                        CMADThreadHandler::s_MADEvent(TRUE, FALSE, "AcmeMADEvent");
LPTOP_LEVEL_EXCEPTION_FILTER     CMADThreadHandler::s_pPrevUnhandledExceptionFilter = 0;

CMADThreadHandler::CMADThreadHandler( CMclEvent& ExitEvent )
    : m_ExitEvent(ExitEvent)
{
    s_pPrevUnhandledExceptionFilter =
        SetUnhandledExceptionFilter(UnhandledExceptionFilter);
}

CMADThreadHandler::~CMADThreadHandler()
{
    SetUnhandledExceptionFilter(s_pPrevUnhandledExceptionFilter);
}

unsigned CMADThreadHandler::ThreadHandlerProc( void )
{
    DWORD dwWaitResult;
    
    printf(
        "[0x%08lx] MAD thread is starting.\n",
        GetCurrentThreadId()
    );

    dwWaitResult = m_ExitEvent.WaitForTwo(s_MADEvent, FALSE, INFINITE);

    if( CMclWaitSucceeded(dwWaitResult, 2) )
    {
        if( CMclWaitSucceededIndex(dwWaitResult) == 0 )
        {
            printf(
                "[0x%08lx] MAD thread exiting normally.\n",
                GetCurrentThreadId()
            );
        }
        else
        {
            printf(
                "[0x%08lx] MAD event set - terminating process.\n",
                GetCurrentThreadId()
            );

            // Notify other threads in this process that an exception
            // occurred somewhere in the product.
            //

            // Terminate the process.
            //
            exit(-1);
        }
    }
    else
    {
        printf(
            "[0x%08lx] MAD thread failed to execute.\n",
            GetCurrentThreadId()
        );
    }

    return(0);
}

LONG WINAPI CMADThreadHandler::UnhandledExceptionFilter( PEXCEPTION_POINTERS pExceptionInfo )
{
    printf(
        "[0x%08lx] MAD thread's unhandled exception filter is about to set MAD event.\n",
        GetCurrentThreadId()
    );

    s_MADEvent.Set();

    return (* s_pPrevUnhandledExceptionFilter)(pExceptionInfo);
}

⌨️ 快捷键说明

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