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.
5 // Simple thread pool class
7 #ifndef LIBRARIES_SDK_UTIL_THREAD_POOL_H_
8 #define LIBRARIES_SDK_UTIL_THREAD_POOL_H_
11 #include <semaphore.h>
13 #include "sdk_util/atomicops.h"
17 // typdef helper for work function
18 typedef void (*WorkFunction
)(int task_index
, void* data
);
20 // ThreadPool is a class to manage num_threads and assign
21 // them num_tasks of work at a time. Each call
22 // to Dispatch(..) will block until all tasks complete.
23 // If 0 is passed in for num_threads, all tasks will be
24 // issued on the dispatch thread.
28 void Dispatch(int num_tasks
, WorkFunction work
, void* data
);
29 explicit ThreadPool(int num_threads
);
33 void Setup(int counter
, WorkFunction work
, void* data
);
34 void DispatchMany(int num_tasks
, WorkFunction work
, void* data
);
35 void DispatchHere(int num_tasks
, WorkFunction work
, void* data
);
37 static void* WorkerThreadEntry(void* data
);
38 void PostExitAndJoinAll();
41 const int num_threads_
;
44 WorkFunction user_work_function_
;
49 } // namespace sdk_util
51 #endif // LIBRARIES_SDK_UTIL_THREAD_POOL_H_