# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

thread_win32.cpp

Go to the documentation of this file.
00001 
00002 //#include "Core/precomp.h"
00003 
00004 #include "thread.h"
00005 #include "thread_win32.h"
00006 
00007 /*************************************
00008  Generic static create-thread function
00009 *************************************/
00010 
00011 CL_Thread *CL_Thread::create(CL_Runnable *runnable)
00012 {
00013         return new CL_Thread_Win32(runnable);
00014 }
00015 
00016 /*************************************
00017         class CL_Thread_Win32
00018 *************************************/
00019 
00020 
00021 CL_Thread_Win32::CL_Thread_Win32(CL_Runnable *runnable)
00022 {
00023         this->runnable = runnable;
00024         thread_handle = NULL;
00025 }
00026 
00027 CL_Thread_Win32::~CL_Thread_Win32()
00028 {
00029         if (thread_handle != NULL) terminate();
00030 }
00031 
00032 unsigned long CL_Thread_Win32::func_proxy(void *arg)
00033 {
00034         CL_Thread_Win32 *parent = (CL_Thread_Win32 *) arg;
00035         parent->runnable->run();
00036 
00037         return 0;
00038 }
00039 
00040 void CL_Thread_Win32::start()
00041 {
00042         thread_handle = CreateThread(NULL, 0, func_proxy, this, 0, &thread_id);
00043         if (thread_handle == NULL)
00044         {
00045                 //throw CL_Error("Failed to create thread");
00046         }
00047 }
00048 
00049 void CL_Thread_Win32::terminate()
00050 {
00051         TerminateThread(thread_handle, 0);
00052         CloseHandle(thread_handle);
00053         thread_handle = NULL;
00054 }
00055 
00056 void CL_Thread_Win32::wait()
00057 {
00058         if (thread_handle == NULL) return;
00059 
00060         WaitForSingleObject(thread_handle, INFINITE);
00061         CloseHandle(thread_handle);
00062         thread_handle = NULL;
00063 }