Friday, February 14, 2014

Thread windows style

I found this WINgnam style implementation on the web:

#ifndef _WIN_T_H_
#define _WIN_T_H_ 1
#include <iostream>
nclude <cassert>
#include <memory> # i#include <windows.h>
class Runnable { pub
#include <process.h> lic:
virtual ~Runnable() = 0;
virtual void* run() = 0; }; class Thread { public:
le> run); Thread(); virtual ~Thread
Thread(std::auto_ptr<Runna b(); void start(); void* join(); private: HANDLE hThread;
matically std::auto
unsigned wThreadID; // runnable object will be deleted aut o_ptr<Runnable> runnable; Thread(const Thread&);
d when run() completes void setComplete
const Thread& operator=(const Thread&); // call ed(); // stores return value from run() void* result; virtual void* run() {return 0;}
hread(LPVOID pVoid); void printError(LPSTR lpszFunction,
static unsigned WINAPI startThreadRunnable(LPVOID pVoid); static unsigned WINAPI start TLPSTR fileName, int lineNumber); }; class simpleRunnable: public Runnable { public: simpleRunnable(int ID) : myID(ID) {} virtual void* run() {
ad: public Thread { public: simpleThread(int ID) : myID(ID) {}
std::cout << "Thread " << myID << " is running" << std::endl; return reinterpret_cast<void*>(myID); } private: int myID; }; class simpleThr e virtual void* run() { std::cout << "Thread " << myID << " is running" << std::endl; return reinterpret_cast<void*>(myID); } private: int myID; };
#endif

No comments:

Post a Comment