Windows多线程技术研究(四):跨平台线程API
线程是跟操作系统有关系的,所以Windows和Linux下线程的API有些差别,为了实现线程跨平台,于是整理了下面两个文件,Thread.h和Thread.cpp。用来实现跨平台线程API。
TestMain.cpp文件是测试跨平台线程API的。
注意:测试的时候运行时库选择多线程调试或多线程。
//Thread.h
#ifndef __THREAD_H__
#define __THREAD_H__
#ifdef WIN32 //define the macro for Win32 thread
#include <windows.h>
#include <process.h>
/************* mutex (use CRITICAL_SECTION in windows) ***************/
#define THREAD_MUTEX CRITICAL_SECTION
#define INITIALIZE_MUTEX(mutex) InitializeCriticalSection(mutex)
#define DESTROY_MUTEX DeleteCriticalSection
#define LOCK_MUTEX EnterCriticalSection
#define UNLOCK_MUTEX LeaveCriticalSection
#define THREAD_HANDLE unsigned long
/**********************************************************************/
typedef unsigned int (__stdcall * THREAD_FUN_TYPE)(void *);
#else //define the macro for POSIX thread
#include <pthread.h>
/************* mutex (use CRITICAL_SECTION in windows) ***************/
#define THREAD_MUTEX pthread_mutex_t
#define INITIALIZE_MUTEX(mutex) pthread_mutex_init(mutex, NULL)
#define DESTROY_MUTEX pthread_mutex_destroy
#define LOCK_MUTEX pthread_mutex_lock
#define UNLOCK_MUTEX pthread_mutex_unlock
#define THREAD_HANDLE pthread_t
/**********************************************************************/
typedef void *( * THREAD_FUN_TYPE)(void *);
#endif //WIN32
/***********************************************************************
** Function : This function is used to create a new thread. The new thread's
function has the form as follows:
int (* pThreadFun) (void *)
(one argument which is void * and return an integer type data)
** Input: pThreadFun -- the address of function to create a new thread
** pParam -- the parament of the new thread
** return the handle of the new thread
************************************************************************/
inline extern THREAD_HANDLE CREATE_THREAD(int (* pThreadFun)( void * ), void * pParam);
/***********************************************************************
** Function : This function is used to end a thread. This function is
usually used at the end of the Thread function.
************************************************************************/
inline extern void END_THREAD();
/***********************************************************************
** Function : This function is used to close the handle of a thread.
** input : hThread -- the handle of the thread.
************************************************************************/
inline extern void CLOSE_THREAD(THREAD_HANDLE hThread);
/***********************************************************************
** Function : This function is used to wait for another thread.
So the main thread used this function will suspend
until the waiting thread finished.
** input : hThread -- the handle of waiting thread.
************************************************************************/
inline extern void WAIT_THREAD(THREAD_HANDLE hThread);
#endif //__THREAD_H__
//Thread.cpp
#include "Thread.h"
#ifdef WIN32 //define the macro for Win32 thread
/***********************************************************************
** Function : This function is used to create a new thread. The new thread's
function has the form as follows:
int (* pThreadFun) (void *)
(one argument which is void * and return an integer type data)
** Input: pThreadFun -- the address of function to create a new thread
** pParam -- the parament of the new thread
** return the handle of the new thread
************************************************************************/
inline THREAD_HANDLE CREATE_THREAD(int (* pThreadFun)( void * ), void * pParam)
{
return _beginthreadex(NULL, 0, (THREAD_FUN_TYPE) pThreadFun, pParam, NULL, NULL);
}
/***********************************************************************
** Function : This function is used to end a thread. This function is
usually used at the end of the Thread function.
************************************************************************/
inline void END_THREAD()
{
_endthreadex(0);
}
/***********************************************************************
** Function : This function is used to close the handle of a thread.
** input : hThread -- the handle of the thread.
************************************************************************/
inline void CLOSE_THREAD(THREAD_HANDLE hThread)
{
CloseHandle((HANDLE *) hThread);
}
/***********************************************************************
** Function : This function is used to wait for another thread.
So the main thread used this function will suspend
until the waiting thread finished.
** input : hThread -- the handle of waiting thread.
************************************************************************/
inline void WAIT_THREAD(THREAD_HANDLE hThread)
{
WaitForSingleObject((HANDLE*)hThread, INFINITE);
}
#else //define the macro for POSIX thread
/***********************************************************************
** Function : This function is used to create a new thread. The new thread's
function has the form as follows:
int (* pThreadFun) (void *)
(one argument which is void * and return an integer type data)
** Input: pThreadFun -- the address of function to create a new thread
** pParam -- the parament of the new thread
** return the handle of the new thread
************************************************************************/
inline THREAD_HANDLE CREATE_THREAD(int (* pThreadFun)( void * ), void * pParam)
{
pthread_t threadID;
pthread_create(&threadID, NULL, (THREAD_FUN_TYPE)pThreadFun, pParam);
return threadID;
}
/***********************************************************************
** Function : This function is used to end a thread. This function is
usually used at the end of the Thread function.
************************************************************************/
inline void END_THREAD()
{
pthread_exit(0);
}
/***********************************************************************
** Function : This function is used to close the handle of a thread.
But the POSIX thread use the pthread_join function to
clean the handle of the thread. So this funciton do
nothing in Linux platform.
************************************************************************/
inline void CLOSE_THREAD(THREAD_HANDLE hThread)
{
}
/***********************************************************************
** Function : This function is used to wait for another thread.
So the main thread used this function will suspend
until the waiting thread finished.
** input : hThread -- the handle of waiting thread.
************************************************************************/
inline void WAIT_THREAD(THREAD_HANDLE hThread)
{
void * retval;
pthread_join(hThread, &retval);
}
#endif //WIN32
//TestMain.cpp
#ifdef _WIN32
#include <stdio.h>
#include <stdlib.h>
#else
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#endif
#include "Thread.h"
//create the global mutex
THREAD_MUTEX g_Mutex;
//parament of the test thread
struct TParam
{
int iNum;
int ThreadID;
};
//test thread
int testFun(void * pParam)
{
//lock the mutex : so only one thread can visit the below source at one time
LOCK_MUTEX(&g_Mutex);
TParam * pTParam = (TParam *)pParam;
printf("Start Thread ID: %d/n", pTParam->ThreadID);
#ifdef WIN32
Sleep(pTParam->iNum * 1000);
#else
sleep(pTParam->iNum);
#endif
printf("Leave Thread ID: %d /n", pTParam->ThreadID);
//unlock the mutex : let other thread use up source
UNLOCK_MUTEX(&g_Mutex);
//end the thread
END_THREAD();
return 1;
}
//number of created threads
#define THREAD_NUM 4
int main()
{
//initialize the mutex
INITIALIZE_MUTEX(&g_Mutex);
TParam Param[THREAD_NUM];
THREAD_HANDLE hThread[THREAD_NUM];
int i = 0;
for (i = 0; i < THREAD_NUM; i++)
{
Param[i].iNum = 1 + rand() % 5;
Param[i].ThreadID = i;
hThread[i] = CREATE_THREAD(testFun, &Param[i]);
}
//wait all threads to finish
for (i = 0; i < THREAD_NUM; i++)
{
WAIT_THREAD(hThread[i]);
CLOSE_THREAD(hThread[i]);
}
printf("All Threads have been closed./n");
//destroy the mutex
DESTROY_MUTEX(&g_Mutex);
return 1;
}