-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.h
162 lines (133 loc) · 2.83 KB
/
Thread.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* File: Thread.h
* Author: maxds
*
* Created on 4 novembre 2011, 18:12
*/
#ifndef THREAD_H
#define THREAD_H
#include <pthread.h>
#include <cstdlib>
#include <queue>
namespace Threading{
typedef pthread_t Thread;
typedef pthread_cond_t Vcondition;
typedef pthread_attr_t ThreadAttributs;
typedef pthread_condattr_t VconditionAttributs;
typedef pthread_mutex_t Mutex_t;
typedef pthread_mutexattr_t Mutex_t_Attributs;
namespace Condition
{
/**
*
* @param lock
* @param vc
* @return
*/
int wait(Mutex_t* lock,Vcondition* vc){
return pthread_cond_wait(vc,lock);
}
/**
*
* @param vc
* @return
*/
int signal(Vcondition* vc){
return pthread_cond_signal(vc);
}
/**
*
* @param c
* @return
*/
int init(Vcondition* c){
return pthread_cond_init(c,NULL);
}
/**
*
* @param c
* @return
*/
int destroy(Vcondition *c){
return pthread_cond_destroy(c);
}
}
namespace Mutex{
/**
*
* @param m
* @return
*/
int init(Mutex_t * m){
return pthread_mutex_init(m,NULL);
}
/**
*
* @param m
* @return
*/
int destroy(Mutex_t * m){
return pthread_mutex_destroy(m);
}
/**
*
* @param m
* @return
*/
int lock(Mutex_t *m){
return pthread_mutex_lock(m);
}
/**
*
* @param m
* @return
*/
int unlock(Mutex_t* m){
return pthread_mutex_unlock(m);
}
}
/**
* Retourne l'identifant du thread appelant
*/
unsigned int get_thread_id(){
return (unsigned int) pthread_self();
}
/**
* Crée un thread simple sans propriétés d'ordonnancement
* @param routine
* @param args
* @return
*/
Thread* create_thread(void* (*routine) (void*), void* args){
Thread* t = new Thread;
int ret=pthread_create(t,NULL,routine,args);
return (ret==0)?t:NULL;
}
/**
*
* @param t
* @param routine
* @param args
* @return
*/
int create_thread(Thread* t,void* (*routine) (void*), void* args){
int ret=pthread_create(t,NULL,routine,args);
return ret;
}
/**
*
* @param t
* @return
*/
int join_thread(Thread& t){
return pthread_join(t,NULL);
}
/**
*
* @param s
*/
void sleep_thread(unsigned int s){
sleep(s);
}
}
#endif /* THREAD_H */