Skip to content

Commit 813d456

Browse files
committed
use std::thread instead of system specific implementation (#480)
1 parent 9f8f5c3 commit 813d456

File tree

2 files changed

+8
-66
lines changed

2 files changed

+8
-66
lines changed

libde265/threads.cc

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,6 @@
2929
#endif
3030

3131

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-
6332
de265_progress_lock::de265_progress_lock()
6433
{
6534
mProgress = 0;
@@ -151,11 +120,8 @@ void printblks(const thread_pool* pool)
151120
#endif
152121

153122

154-
static THREAD_RESULT_TYPE THREAD_CALLING_CONVENTION worker_thread(THREAD_PARAM_TYPE pool_ptr)
123+
static void worker_thread(thread_pool* pool)
155124
{
156-
thread_pool* pool = (thread_pool*)pool_ptr;
157-
158-
159125
while(true) {
160126

161127
thread_task* task = nullptr;
@@ -179,7 +145,7 @@ static THREAD_RESULT_TYPE THREAD_CALLING_CONVENTION worker_thread(THREAD_PARAM_T
179145
// if the pool was shut down, end the execution
180146

181147
if (pool->stopped) {
182-
return (THREAD_RESULT_TYPE)0;
148+
return;
183149
}
184150

185151

@@ -204,8 +170,6 @@ static THREAD_RESULT_TYPE THREAD_CALLING_CONVENTION worker_thread(THREAD_PARAM_T
204170

205171
pool->num_threads_working--;
206172
}
207-
208-
return (THREAD_RESULT_TYPE)0;
209173
}
210174

211175

@@ -232,12 +196,8 @@ de265_error start_thread_pool(thread_pool* pool, int num_threads)
232196
// start worker threads
233197

234198
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);
241201
pool->num_threads++;
242202
}
243203

@@ -255,8 +215,7 @@ void stop_thread_pool(thread_pool* pool)
255215
pool->cond_var.notify_all();
256216

257217
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();
260219
}
261220
}
262221

libde265/threads.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@
3131
#include <string>
3232
#include <atomic>
3333

34-
#ifndef _WIN32
35-
#include <pthread.h>
36-
37-
typedef pthread_t de265_thread;
38-
39-
#else // _WIN32
34+
#ifdef _WIN32
4035
#if !defined(NOMINMAX)
4136
#define NOMINMAX 1
4237
#endif
@@ -45,23 +40,11 @@ typedef pthread_t de265_thread;
4540
#if _MSC_VER > 1310
4641
#include <intrin.h>
4742
#endif
48-
49-
typedef HANDLE de265_thread;
5043
#endif // _WIN32
5144

5245
#include <mutex>
5346
#include <condition_variable>
54-
//#include <thread>
55-
56-
//typedef pthread_t de265_thread;
57-
58-
#ifndef _WIN32
59-
int de265_thread_create(de265_thread* t, void *(*start_routine) (void *), void *arg);
60-
#else
61-
int de265_thread_create(de265_thread* t, LPTHREAD_START_ROUTINE start_routine, void *arg);
62-
#endif
63-
void de265_thread_join(de265_thread t);
64-
void de265_thread_destroy(de265_thread* t);
47+
#include <thread>
6548

6649
class de265_progress_lock
6750
{
@@ -115,7 +98,7 @@ class thread_pool
11598

11699
std::deque<thread_task*> tasks; // we are not the owner
117100

118-
de265_thread thread[MAX_THREADS];
101+
std::thread thread[MAX_THREADS];
119102
int num_threads;
120103

121104
int num_threads_working;

0 commit comments

Comments
 (0)