-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadTools.h
78 lines (63 loc) · 2.98 KB
/
ThreadTools.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef _THREADTOOLS_H_
#define _THREADTOOLS_H_
#include "ToolsDefine.h"
TOOLSPACE_BEGIN
#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 mutex in Unix like) ***********************/
#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
class ThreadTools
{
public:
/***********************************************************************
** 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 parameter of the new thread
** Return: The handle of the new thread
************************************************************************/
static 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.
************************************************************************/
static void END_THREAD();
/***********************************************************************
** Function: This function is used to close the handle of a thread.
** input: hThread -- the handle of the thread.
************************************************************************/
static 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.
************************************************************************/
static void WAIT_THREAD(THREAD_HANDLE hThread);
protected:
private:
}; // ThreadTools
TOOLSPACE_END
#endif