|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Simple thread pool class | |
6 | |
7 #ifndef EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ | |
8 #define EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ | |
9 | |
10 #if !defined(DISABLE_THREADS) | |
11 #include <pthread.h> | |
12 #include <semaphore.h> | |
13 #endif | |
14 | |
15 // typdef helper for work function | |
16 typedef void (*WorkFunction)(int task_index, void *data); | |
17 | |
18 // ThreadPool is a class to manage num_threads and assign | |
19 // them num_tasks of work at a time. Each call | |
20 // to Dispatch(..) will block until all tasks complete. | |
21 | |
22 class ThreadPool { | |
23 #if !defined(DISABLE_THREADS) | |
noelallen1
2013/05/25 00:21:11
I would recommend removing. We either should sup
nfullagar1
2013/05/28 23:11:50
removed
| |
24 public: | |
25 void Dispatch(int num_tasks, WorkFunction work, void *data); | |
26 explicit ThreadPool(int num_threads); | |
27 ~ThreadPool(); | |
28 private: | |
29 int DecCounter(); | |
30 void Setup(int counter, WorkFunction work, void *data); | |
31 void MultiThread(int num_tasks, WorkFunction work, void *data); | |
32 void SingleThread(int num_tasks, WorkFunction work, void *data); | |
33 void WorkLoop(); | |
34 static void* WorkerThreadEntry(void *data); | |
35 void PostExitAndJoinAll(); | |
36 pthread_t *threads_; | |
binji
2013/05/24 21:47:55
might want to run clang-format on this diff to fix
nfullagar1
2013/05/28 23:11:50
Done.
| |
37 int counter_; | |
38 int num_threads_; | |
noelallen1
2013/05/25 00:21:11
Did you mean to make this one const?
nfullagar1
2013/05/28 23:11:50
yes! thx.
| |
39 bool exiting_; | |
40 void *user_data_; | |
41 WorkFunction user_work_function_; | |
42 pthread_mutex_t mutex_; | |
43 sem_t work_tasks_; | |
44 sem_t done_tasks_; | |
45 #else | |
46 public: | |
47 void Dispatch(int num_tasks, WorkFunction work, void *data) { | |
48 for(int i = 0; i < num_tasks; i++) work(i, data); } | |
binji
2013/05/24 21:47:55
share with SingleThread?
nfullagar1
2013/05/28 23:11:50
Fishing this out and sharing this one line of code
| |
49 explicit ThreadPool(int num_threads) { ; } | |
binji
2013/05/24 21:47:55
nit: {}
noelallen1
2013/05/25 00:21:11
A bit strange that in the DISABLE_THREADS case the
nfullagar1
2013/05/28 23:11:50
Done.
nfullagar1
2013/05/28 23:11:50
DISABLE_THREADS removed
| |
50 ~ThreadPool() { ; } | |
51 #endif // DISABLE_THREADS | |
52 }; | |
53 #endif // EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ | |
OLD | NEW |