madmadworld.cpp
来自「window下的多线程编程参考书。值得一读」· C++ 代码 · 共 71 行
CPP
71 行
//
// FILE: MadMadWorld.cpp
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////
#include <CMclGlobal.h>
#include <CMclThread.h>
#include <CMclEvent.h>
#include <CMclWaitableCollection.h>
#include <stdio.h>
#include "CWorkerThreadHandler.h"
#include "CMADThreadHandler.h"
void main( int iNumArgs )
{
printf(
"[0x%08lx] MadMadWorld starting up.\n",
GetCurrentThreadId()
);
// Create the event that will be used to tell all our threads
// (including the MAD thread) to exit normally.
//
CMclEvent ExitEvent(TRUE, FALSE);
// Create the MAD thread.
//
CMADThreadHandler MADThreadHandler(ExitEvent);
CMclThread MADThread(&MADThreadHandler);
// Create the first worker thread.
//
CWorkerThreadHandler ThreadHandler1(ExitEvent, FALSE);
CMclThread Thread1(&ThreadHandler1);
// Create the second worker thread (the one that might fault).
//
CWorkerThreadHandler ThreadHandler2(ExitEvent, (iNumArgs > 1));
CMclThread Thread2(&ThreadHandler2);
// Do some work for 10 seconds.
//
Sleep(10000);
printf(
"[0x%08lx] MadMadWorld notifying worker threads to exit.\n",
GetCurrentThreadId()
);
// Tell the MAD and worker threads to exit.
//
ExitEvent.Set();
// Wait for the threads to exit before terminating the process.
//
CMclWaitableCollection AppThreads;
AppThreads.AddObject(MADThread);
AppThreads.AddObject(Thread1);
AppThreads.AddObject(Thread2);
AppThreads.Wait(TRUE, INFINITE);
printf(
"[0x%08lx] MadMadWorld exiting.\n",
GetCurrentThreadId()
);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?