www.pudn.com > DVBPlayer.rar > win32thread.cpp, change:2004-07-15,size:2345b


#include <process.h> 
 
#include "win32thread.h" 
 
int32 priority_values[] = { 
    -15, 
    -2, 
    -1, 
    0, 
    1, 
    2, 
    15 
}; 
 
const uint32 kNumPriorities = sizeof(priority_values)/sizeof(int32); 
 
win32Thread:: 
win32Thread(): 
Thread() 
{ 
	m_threadHandle	= NULL;	 
	m_threadId		= 0; 
    m_function      = NULL; 
    m_arg           = NULL; 
} 
 
win32Thread:: 
~win32Thread() 
{ 
    if(m_threadHandle) 
    { 
	    CloseHandle(m_threadHandle); 
    } 
} 
 
unsigned long __stdcall  
win32Thread:: 
internalThreadFunction(void* arg) 
{ 
    win32Thread* thread = (win32Thread*) arg; 
 
    return thread->InternalThreadFunction(); 
} 
 
uint32  
win32Thread:: 
InternalThreadFunction() 
{ 
    m_function(m_arg); 
 
    return 0; 
} 
 
bool  
win32Thread:: 
Create(thread_function function, void* arg, bool detach) 
{ 
	bool result = false; 
 
    m_function      = function; 
    m_arg           = arg; 
 
	m_threadHandle = ::CreateThread(NULL, 
									0, 
									internalThreadFunction, 
									this, 
									0, 
									&m_threadId); 
	//m_threadHandle = (HANDLE) _beginthreadex( 
	//								NULL, 
	//								0, 
	//								(unsigned int (__stdcall *)(void *))internalThreadFunction, 
	//								this, 
	//								0, 
	//								(unsigned int *)&m_threadId); 
	if(m_threadHandle) 
	{ 
		result = true; 
	} 
	 
	return result; 
} 
 
void  
win32Thread:: 
Destroy() 
{ 
	 TerminateThread(m_threadHandle, 0); 
} 
 
void  
win32Thread:: 
Suspend() 
{ 
	 SuspendThread(m_threadHandle); 
} 
 
void  
win32Thread:: 
Resume() 
{ 
	ResumeThread(m_threadHandle); 
} 
 
void 
win32Thread:: 
Join() 
{ 
	WaitForSingleObject(m_threadHandle, INFINITE); 
} 
 
uint32  
win32Thread:: 
GetPriority() const 
{ 
    int32 priority = GetThreadPriority(m_threadHandle); 
 
    for(int32 i=0; i < kNumPriorities; i++) 
    { 
        if(priority_values[i] == priority) 
        { 
            priority = i; 
            break; 
        } 
    } 
 
	return priority; 
} 
 
uint32  
win32Thread:: 
SetPriority(uint32 priority) 
{ 
	int32 old = GetThreadPriority(m_threadHandle); 
 
	SetThreadPriority(m_threadHandle, priority_values[priority]); 
 
    for(int32 i=0; i < kNumPriorities; i++) 
    { 
        if(priority_values[i] == old) 
        { 
            old = i; 
            break; 
        } 
    } 
 
	return old; 
}