29
29
#endif
30
30
31
31
32
- #ifndef _WIN32
33
- // #include <intrin.h>
34
-
35
- #define THREAD_RESULT_TYPE void *
36
- #define THREAD_PARAM_TYPE void *
37
- #define THREAD_CALLING_CONVENTION
38
-
39
- #include < stdio.h>
40
-
41
- int de265_thread_create (de265_thread* t, void *(*start_routine) (void *), void *arg) { return pthread_create (t,NULL ,start_routine,arg); }
42
- void de265_thread_join (de265_thread t) { pthread_join (t,NULL ); }
43
- void de265_thread_destroy (de265_thread* t) { }
44
- #else // _WIN32
45
-
46
- #define THREAD_RESULT_TYPE DWORD
47
- #define THREAD_CALLING_CONVENTION WINAPI
48
- #define THREAD_PARAM_TYPE LPVOID
49
-
50
- int de265_thread_create (de265_thread* t, LPTHREAD_START_ROUTINE start_routine, void *arg) {
51
- HANDLE handle = CreateThread (NULL , 0 , start_routine, arg, 0 , NULL );
52
- if (handle == NULL ) {
53
- return -1 ;
54
- }
55
- *t = handle;
56
- return 0 ;
57
- }
58
- void de265_thread_join (de265_thread t) { WaitForSingleObject (t, INFINITE); }
59
- void de265_thread_destroy (de265_thread* t) { CloseHandle (*t); *t = NULL ; }
60
- #endif // _WIN32
61
-
62
-
63
32
de265_progress_lock::de265_progress_lock ()
64
33
{
65
34
mProgress = 0 ;
@@ -151,11 +120,8 @@ void printblks(const thread_pool* pool)
151
120
#endif
152
121
153
122
154
- static THREAD_RESULT_TYPE THREAD_CALLING_CONVENTION worker_thread (THREAD_PARAM_TYPE pool_ptr )
123
+ static void worker_thread (thread_pool* pool )
155
124
{
156
- thread_pool* pool = (thread_pool*)pool_ptr;
157
-
158
-
159
125
while (true ) {
160
126
161
127
thread_task* task = nullptr ;
@@ -179,7 +145,7 @@ static THREAD_RESULT_TYPE THREAD_CALLING_CONVENTION worker_thread(THREAD_PARAM_T
179
145
// if the pool was shut down, end the execution
180
146
181
147
if (pool->stopped ) {
182
- return (THREAD_RESULT_TYPE) 0 ;
148
+ return ;
183
149
}
184
150
185
151
@@ -204,8 +170,6 @@ static THREAD_RESULT_TYPE THREAD_CALLING_CONVENTION worker_thread(THREAD_PARAM_T
204
170
205
171
pool->num_threads_working --;
206
172
}
207
-
208
- return (THREAD_RESULT_TYPE)0 ;
209
173
}
210
174
211
175
@@ -232,12 +196,8 @@ de265_error start_thread_pool(thread_pool* pool, int num_threads)
232
196
// start worker threads
233
197
234
198
for (int i=0 ; i<num_threads; i++) {
235
- int ret = de265_thread_create (&pool->thread [i], worker_thread, pool);
236
- if (ret != 0 ) {
237
- // cerr << "pthread_create() failed: " << ret << endl;
238
- return DE265_ERROR_CANNOT_START_THREADPOOL;
239
- }
240
-
199
+ printf (" start thread %d\n " , i);
200
+ pool->thread [i] = std::thread (worker_thread, pool);
241
201
pool->num_threads ++;
242
202
}
243
203
@@ -255,8 +215,7 @@ void stop_thread_pool(thread_pool* pool)
255
215
pool->cond_var .notify_all ();
256
216
257
217
for (int i=0 ;i<pool->num_threads ;i++) {
258
- de265_thread_join (pool->thread [i]);
259
- de265_thread_destroy (&pool->thread [i]);
218
+ pool->thread [i].join ();
260
219
}
261
220
}
262
221
0 commit comments