//定义了与多任务(多线程)有关的平台无关的接口
//适合平台:WindowsXP, WindowsCE, Linux, Android
#ifndef _MINI_SUN_PLATFORM_MULTI_TASK_H_
#define _MINI_SUN_PLATFORM_MULTI_TASK_H_
//对Android平台,也认为是linux
#ifdef ANDROID
#define linux ANDROID
#endif
//包含头文件
#ifdef _WIN32
#include
#endif
#ifdef linux
#include
#include
#endif
//任务(线程)函数参数类型:TypeTaskArg
#ifdef _WIN32
typedef LPVOID TypeTaskArg;
#endif
#ifdef linux
typedef void* TypeTaskArg;
#endif
//任务(线程)函数声明类型:TypeTaskDec
#ifdef _WIN32
#define TypeTaskDec DWORD WINAPI
#endif
#ifdef linux
#define TypeTaskDec void*
#endif
//任务(线程)函数返回值类型:TypeTaskReturn
#ifdef _WIN32
typedef DWORD TypeTaskReturn;
#endif
#ifdef linux
typedef void* TypeTaskReturn;
#endif
//任务句柄类型:TypeTask
#ifdef _WIN32
typedef HANDLE TypeTask;
#endif
#ifdef linux
typedef pthread_t TypeTask;
#endif
//启动新任务的方法
//bool TaskCreateNew(TypeTask&, TypeTaskDec (*TaskFun)(TypeTaskArg), TypeTaskArg&); //成功返回true
#ifdef _WIN32
#define TaskCreateNew(t, f, a) (((t) = CreateThread(NULL, 0, f, a, 0, NULL)) != NULL)
#endif
#ifdef linux
#define TaskCreateNew(t, f, a) (!pthread_create(&(t), NULL, f, a))
#endif
//等待任务结束的方法
//bool TaskWaitToFinish(TypeTask&); //成功返回true
#ifdef _WIN32
#define TaskWaitToFinish(t) ((WaitForSingleObject(t, INFINITE) == WAIT_OBJECT_0) && CloseHandle(t))
#endif
#ifdef linux
#define TaskWaitToFinish(t) (!pthread_join(t, NULL))
#endif
//任务睡眠一段时间的方法
//void TaskSleep(unsigned long ms); //睡眠ms毫秒
#ifdef _WIN32
#define TaskSleep(ms) Sleep(ms);
#endif
#ifdef linux
#define TaskSleep(ms) usleep((ms) * 1000)
#endif
//互斥量类型:TypeMutex
#ifdef _WIN32
typedef HANDLE TypeMutex;
#endif
#ifdef linux
typedef pthread_mutex_t TypeMutex;
#endif
//创建(初始化)互斥量的方法
//bool MutexInit(TypeMutex&); //成功返回true
#ifdef _WIN32
#define MutexInit(m) (((m) = CreateMutex(NULL, FALSE, NULL)) != NULL)
#endif
#ifdef linux
#define MutexInit(m) (!pthread_mutex_init(&(m), NULL))
#endif
//销毁互斥量的方法
//bool MutexDestroy(TypeMutex&); //成功返回true
#ifdef _WIN32
#define MutexDestroy(m) CloseHandle(m)
#endif
#ifdef linux
#define MutexDestroy(m) (!pthread_mutex_destroy(&(m)))
#endif
//对互斥量上锁的方法
//bool MutexLock(TypeMutex&); //成功返回true
#ifdef _WIN32
#define MutexLock(m) (WaitForSingleObject(m, INFINITE) == WAIT_OBJECT_0)
#endif
#ifdef linux
#define MutexLock(m) (!pthread_mutex_lock(&(m)))
#endif
//对互斥量解锁的方法
//bool MutexUnlock(TypeMutex&); //成功返回true
#ifdef _WIN32
#define MutexUnlock(m) ReleaseMutex(m)
#endif
#ifdef linux
#define MutexUnlock(m) (!pthread_mutex_unlock(&(m)))
#endif
#endif //_MINI_SUN_PLATFORM_MULTI_TASK_H_
阅读(970) | 评论(0) | 转发(0) |