Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related thread1.h00001 // 00002 // SYNTOPIA. See http://Syntopia.sourceforge.net for details and documentation. 00003 // 00004 // 00005 // 00006 00007 // This thread code is taken from C/C++ User Journal. See: 00008 // 00009 // http://www.cuj.com/articles/1999/9912/9912d/9912dl1.htm?topic=articles 00010 // 00011 // for more information. 00012 00013 00014 #include <winbase.h> // for CreateThread and macros 00015 00016 namespace MSVC 00017 { 00018 00019 00020 template <typename Object> 00021 void runInThread( 00022 Object& objectInstance, 00023 DWORD (WINAPI Object::*memberFunc)(void), 00024 HANDLE* handle=0, 00025 DWORD* id=0) 00026 { 00027 using std::runtime_error; 00028 00029 DWORD (WINAPI *threadFunc)(void*); 00030 00031 // pass object "this" pointer via thread void* argument 00032 void *thisPointer = static_cast<void*>(&objectInstance); 00033 00034 // copy class member pointer to global function pointer 00035 *reinterpret_cast<long*>(&threadFunc) = 00036 *reinterpret_cast<long*>(&memberFunc); 00037 00038 DWORD threadId = -1; 00039 00040 // replace CreateThread if your compiler has a function 00041 // to initialize a thread-safe C runtime library 00042 HANDLE result = 00043 CreateThread( 00044 0, 00045 0, 00046 threadFunc, 00047 thisPointer, 00048 0, 00049 &threadId); 00050 00051 if (result==0) throw runtime_error("CreateThread failed"); 00052 00053 if (handle) *handle = result; 00054 if (id) *id = threadId; 00055 } 00056 00057 } Docs made by Doxygen. Email: Mikael Christensen |