├── Model.txt
├── ReadMe.md
├── ScreenShot.png
├── threadpool.sln
└── threadpool
├── Mutex.cpp
├── Mutex.h
├── Semaphore.cpp
├── Semaphore.h
├── Test.cpp
├── Thread.cpp
├── Thread.h
├── ThreadPoolExecutor.cpp
├── ThreadPoolExecutor.h
└── threadpool.vcproj
/Model.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 | Init初始化 ---> 创建多个CWork存到m_ThreadPool ---> 每个CWork在单独创建线程
4 | ---> 线程执行CThreadPoolExecutor::CWork::Run()循环GetTask() ---> 运行获取到的任务。
5 |
6 |
7 | 是 是
8 | 添加一个任务Execute ---> 任务列表满没了 ---> 线程池线程到达上限了没 ---> 放弃任务,返回false
9 | | |
10 | | 否 | 否
11 | | |
12 | 直接添加到任务列表 创建新线程CWork直接执行任务
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/ReadMe.md:
--------------------------------------------------------------------------------
1 |
2 |
ThreadPool
3 |
4 | 一款设计不错的线程池
5 |
6 |
7 |
8 |
9 | 线程管理:自定义最小,最大并发线程。任务多时,自动增加工作线程。
10 |
11 | 空闲处理:恢复到最先小并发线程,清理多余线程内存
12 |
13 |
14 | 
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ScreenShot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/ScreenShot.png
--------------------------------------------------------------------------------
/threadpool.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 10.00
3 | # Visual Studio 2008
4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "threadpool", "threadpool\threadpool.vcproj", "{BFB506FA-F48C-4883-B4F3-565E38EF6FD0}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|Win32 = Debug|Win32
9 | Release|Win32 = Release|Win32
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {BFB506FA-F48C-4883-B4F3-565E38EF6FD0}.Debug|Win32.ActiveCfg = Debug|Win32
13 | {BFB506FA-F48C-4883-B4F3-565E38EF6FD0}.Debug|Win32.Build.0 = Debug|Win32
14 | {BFB506FA-F48C-4883-B4F3-565E38EF6FD0}.Release|Win32.ActiveCfg = Release|Win32
15 | {BFB506FA-F48C-4883-B4F3-565E38EF6FD0}.Release|Win32.Build.0 = Release|Win32
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | EndGlobal
21 |
--------------------------------------------------------------------------------
/threadpool/Mutex.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Mutex.h"
3 |
4 | struct Mutex::MutexInternal
5 | {
6 | ::CRITICAL_SECTION mtx;
7 | };
8 |
9 | Mutex::Mutex(void):m_pInternal(NULL)
10 | {
11 | m_pInternal = new MutexInternal;
12 | ::InitializeCriticalSection(&m_pInternal->mtx);
13 | }
14 |
15 | Mutex::~Mutex(void)
16 | {
17 | ::DeleteCriticalSection(&m_pInternal->mtx);
18 | }
19 |
20 | bool Mutex::enter()
21 | {
22 | ::EnterCriticalSection(&m_pInternal->mtx);
23 | return true;
24 | }
25 |
26 | bool Mutex::leave()
27 | {
28 | ::LeaveCriticalSection(&m_pInternal->mtx);
29 | return true;
30 | }
31 |
--------------------------------------------------------------------------------
/threadpool/Mutex.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/threadpool/Mutex.h
--------------------------------------------------------------------------------
/threadpool/Semaphore.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/threadpool/Semaphore.cpp
--------------------------------------------------------------------------------
/threadpool/Semaphore.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/threadpool/Semaphore.h
--------------------------------------------------------------------------------
/threadpool/Test.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/threadpool/Test.cpp
--------------------------------------------------------------------------------
/threadpool/Thread.cpp:
--------------------------------------------------------------------------------
1 |
2 | #include "Thread.h"
3 |
4 | CThread::CThread(void) :
5 | m_pRunnable(NULL),
6 | m_bRun(false)
7 | {
8 | }
9 |
10 | CThread::~CThread(void)
11 | {
12 | }
13 |
14 | CThread::CThread(Runnable * pRunnable) :
15 | m_ThreadName(""),
16 | m_pRunnable(pRunnable),
17 | m_bRun(false)
18 | {
19 | }
20 |
21 | CThread::CThread(const char * ThreadName, Runnable * pRunnable) :
22 | m_ThreadName(ThreadName),
23 | m_pRunnable(pRunnable),
24 | m_bRun(false)
25 | {
26 | }
27 |
28 | CThread::CThread(std::string ThreadName, Runnable * pRunnable) :
29 | m_ThreadName(ThreadName),
30 | m_pRunnable(pRunnable),
31 | m_bRun(false)
32 | {
33 | }
34 |
35 | bool CThread::Start(bool bSuspend)
36 | {
37 | if(m_bRun)
38 | {
39 | return true;
40 | }
41 | if(bSuspend)
42 | {
43 | m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, CREATE_SUSPENDED, &m_ThreadID);
44 | }
45 | else
46 | {
47 | m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, 0, &m_ThreadID);
48 | }
49 | m_bRun = (NULL != m_handle);
50 | return m_bRun;
51 | }
52 |
53 | void CThread::Run()
54 | {
55 | if(!m_bRun)
56 | {
57 | return;
58 | }
59 | if(NULL != m_pRunnable)
60 | {
61 | m_pRunnable->Run();
62 | }
63 | m_bRun = false;
64 | }
65 |
66 | void CThread::Join(int timeout)
67 | {
68 | if(NULL == m_handle || !m_bRun)
69 | {
70 | return;
71 | }
72 | if(timeout <= 0)
73 | {
74 | timeout = INFINITE;
75 | }
76 | ::WaitForSingleObject(m_handle, timeout);
77 |
78 | }
79 |
80 | void CThread::Resume()
81 | {
82 | if(NULL == m_handle || !m_bRun)
83 | {
84 | return;
85 | }
86 | ::ResumeThread(m_handle);
87 | }
88 |
89 | void CThread::Suspend()
90 | {
91 | if(NULL == m_handle || !m_bRun)
92 | {
93 | return;
94 | }
95 | ::SuspendThread(m_handle);
96 | }
97 |
98 | bool CThread::Terminate(unsigned long ExitCode)
99 | {
100 | if(NULL == m_handle || !m_bRun)
101 | {
102 | return true;
103 | }
104 | if(::TerminateThread(m_handle, ExitCode))
105 | {
106 | ::CloseHandle(m_handle);
107 | return true;
108 | }
109 | return false;
110 | }
111 |
112 | unsigned int CThread::GetThreadID()
113 | {
114 | return m_ThreadID;
115 | }
116 |
117 | std::string CThread::GetThreadName()
118 | {
119 | return m_ThreadName;
120 | }
121 |
122 | void CThread::SetThreadName(std::string ThreadName)
123 | {
124 | m_ThreadName = ThreadName;
125 | }
126 |
127 | void CThread::SetThreadName(const char * ThreadName)
128 | {
129 | if(NULL == ThreadName)
130 | {
131 | m_ThreadName = "";
132 | }
133 | else
134 | {
135 | m_ThreadName = ThreadName;
136 | }
137 | }
138 |
139 | unsigned int CThread::StaticThreadFunc(void * arg)
140 | {
141 | CThread * pThread = (CThread *)arg;
142 | pThread->Run();
143 | return 0;
144 | }
--------------------------------------------------------------------------------
/threadpool/Thread.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/threadpool/Thread.h
--------------------------------------------------------------------------------
/threadpool/ThreadPoolExecutor.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/threadpool/ThreadPoolExecutor.cpp
--------------------------------------------------------------------------------
/threadpool/ThreadPoolExecutor.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/ThreadPool/93069fa7ca08bc599d8c906a46ab250a1f3783a2/threadpool/ThreadPoolExecutor.h
--------------------------------------------------------------------------------
/threadpool/threadpool.vcproj:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
52 |
55 |
58 |
61 |
68 |
71 |
74 |
77 |
80 |
83 |
86 |
89 |
90 |
98 |
101 |
104 |
107 |
110 |
113 |
124 |
127 |
130 |
133 |
142 |
145 |
148 |
151 |
154 |
157 |
160 |
163 |
164 |
165 |
166 |
167 |
168 |
173 |
176 |
177 |
180 |
181 |
184 |
185 |
188 |
189 |
192 |
193 |
194 |
199 |
202 |
203 |
206 |
207 |
210 |
211 |
214 |
215 |
216 |
221 |
222 |
223 |
224 |
225 |
226 |
--------------------------------------------------------------------------------