dynamitethread.cpp

来自「window下的多线程编程参考书。值得一读」· C++ 代码 · 共 73 行

CPP
73
字号
//
// FILE: DynamiteThread.cpp
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "DynamiteThread.h"

// Dynamite thread's just light a fuse, burn down, and
// then explode by causing an exception to occur.
//
unsigned CDynamiteThread::GuardedThreadHandlerProc( void )
{
    printf(
        "[tid 0x%08lx] Dynamite thread running now - lighting fuse...",
        GetCurrentThreadId()
    );
    
    // Show the fuse burning down...
    //
    for( int i = 0; i < 5; i++ )
    {
        Sleep(1000);
        printf(".");
    }

    printf("!!!\n");

    // Boom!
    //
    *((PBYTE)0) = '!';

    // Will never reach this point.
    //
    printf(
        "[tid 0x%08lx] Dynamite thread exiting normally after explosion\n",
        GetCurrentThreadId()
    );

    return(0);
}

// The virtual exception filter handler is overridden just to display
// the sequence of events.
//
int CDynamiteThread::ExceptionFilter( DWORD dwExceptionCode, LPEXCEPTION_POINTERS pEP )
{
    printf(
        "[tid 0x%08lx] Dynamite exception filter running, exception = 0x%x\n",
        GetCurrentThreadId(),
        dwExceptionCode
    );

    return CGuardedThreadHandler::ExceptionFilter(dwExceptionCode, pEP);
}

// The virtual termination handler is overridden just to display
// the sequence of events.
//
unsigned CDynamiteThread::TerminationHandler( DWORD dwExceptionCode )
{
    printf(
        "[tid 0x%08lx] Dynamite termination handler running, exception = 0x%x\n",
        GetCurrentThreadId(),
        dwExceptionCode
    );

    return CGuardedThreadHandler::TerminationHandler(dwExceptionCode);
}

⌨️ 快捷键说明

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