Listing 3: Main.cpp
/*===========================================================*\
    This is the main entry point for the thread demo.
\*===========================================================*/
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include "MonitorThread.h"
#include "SocketThread.h"
#include "DeviceThread.h"
#include "Miscellaneous.h"

int main(int argc, char* argv[])
{
    #ifdef WIN32
        WSADATA wsaData;
        DWORD status = ::WSAStartup(2, &wsaData);
    #endif

    // All thread objects allocated as locals here.
    MonitorThread   monitor("Monitor", 3000);
    DeviceThread    deviceThread("Device");
    SocketThread    socketThread("Socket");

    // Put pointers into polymorphic array for looping
    std::vector <Win32Thread *> threads;
    threads.push_back(&monitor);
    threads.push_back(&deviceThread);
    threads.push_back(&socketThread);

    // Start all threads - call Start() on each
    std::for_each(threads.begin(), threads.end(), 
        std::mem_fun(Win32Thread::Start));

    Print("All threads started");
    Print("Press Enter to interrupt...");

    // Block this thread, waiting for Enter key
    int ch = std::cin.get();

    // User pressed Enter key.
    // Interrupt all threads - call Signal() on each
    std::for_each(threads.begin(), threads.end(), 
        std::mem_fun(Win32Thread::Signal));

    // Wait for all threads - call WaitToExit() on each
    std::for_each(threads.begin(), threads.end(), 
        std::mem_fun(Win32Thread::WaitToExit));

    Print("All threads complete");

    #ifdef WIN32
        ::WSACleanup();
    #endif

    return 0;
}