-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathThreadPool.h
70 lines (63 loc) · 2.07 KB
/
ThreadPool.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
/****************************** Module Header ******************************\
* Module Name: ThreadPool.h
* Project: CppWindowsService
* Copyright (c) Microsoft Corporation.
*
* The class was designed by Kenny Kerr. It provides the ability to queue
* simple member functions of a class to the Windows thread pool.
*
* Using the thread pool is simple and feels natural in C++.
*
* class FDBService
* {
* public:
*
* void AsyncRun()
* {
* CThreadPool::QueueUserWorkItem(&Service::Run, this);
* }
*
* void Run()
* {
* // Some lengthy operation
* }
* };
*
* Kenny Kerr spends most of his time designing and building distributed
* applications for the Microsoft Windows platform. He also has a particular
* passion for C++ and security programming. Reach Kenny at
* http://weblogs.asp.net/kennykerr/ or visit his Web site:
* http://www.kennyandkarin.com/Kenny/.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
* All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
#pragma once
#include <memory>
class CThreadPool {
public:
template <typename T>
static void QueueUserWorkItem(void (T::*function)(void), T* object, ULONG flags = WT_EXECUTELONGFUNCTION) {
typedef std::pair<void (T::*)(), T*> CallbackType;
auto p = std::make_unique<CallbackType>(function, object);
if (::QueueUserWorkItem(ThreadProc<T>, p.get(), flags)) {
// The ThreadProc now has the responsibility of deleting the pair.
p.release();
} else {
throw GetLastError();
}
}
private:
template <typename T>
static DWORD WINAPI ThreadProc(PVOID context) {
typedef std::pair<void (T::*)(), T*> CallbackType;
std::unique_ptr<CallbackType> p(static_cast<CallbackType*>(context));
(p->second->*p->first)();
return 0;
}
};