Windows多线程技术研究(四):跨平台线程API
来源:互联网 发布:医疗器械软件确认报告 编辑:程序博客网 时间:2024/05/22 05:16
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;
}
- Windows多线程技术研究(四):跨平台线程API
- Windows多线程技术研究(三):线程API
- Windows多线程技术研究(二):多线程基本概念
- Java多线程技术研究(三)-线程池
- Windows多线程技术研究(一):引子
- windows线程API 及多线程
- Java多线程技术研究(二)-线程同步,通信及ThreadLocal
- 单线程和多线程的优缺点(windows平台)
- Windows多线程(四) 经典线程同步 事件Event
- windows平台.lnk文件感染技术研究
- windows 平台 .lnk 文件感染技术研究
- windows平台.lnk文件感染技术研究
- 小字-多线程技术研究(一)
- 跨平台多线程编程教程(二):pthreads API
- Java多线程技术研究(四)-Callable,Future/FutureTask,及Future设计模式
- WINDOWS API 简介(四)
- Windows Store 平台移植 —— (5) 补充线程相关 API
- 基于Windows API的Socket程序开发技术研究(一)广播
- Reference in C++(zz)
- Ajax简介
- Windows 2003服务器安全配置指南
- 网上开店平台选择最关键
- 深入探讨C++中的引用(zz)
- Windows多线程技术研究(四):跨平台线程API
- 我用firefox的经验
- 浅析带const的指针或引用复杂声明
- 指针,爱不起躲得起
- Oralce 备份(Exp/Imp)工具资料整理
- 硬币找零问题(动态规划)
- [转帖]可充电电池知识
- 注意点
- DELPHI在一个窗体中打开另外一个窗体的问题