{"action":"opened","issue":{"url":"https://api.github.com/repos/kevin-shanghai/Programming_Practice/issues/2","labels_url":"https://api.github.com/repos/kevin-shanghai/Programming_Practice/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/kevin-shanghai/Programming_Practice/issues/2/comments","events_url":"https://api.github.com/repos/kevin-shanghai/Programming_Practice/issues/2/events","html_url":"https://github.com/kevin-shanghai/Programming_Practice/issues/2","id":95688120,"number":2,"title":"threadpool.h","user":{"login":"kevin-shanghai","id":2210064,"avatar_url":"https://avatars.githubusercontent.com/u/2210064?v=3","gravatar_id":"","url":"https://api.github.com/users/kevin-shanghai","html_url":"https://github.com/kevin-shanghai","followers_url":"https://api.github.com/users/kevin-shanghai/followers","following_url":"https://api.github.com/users/kevin-shanghai/following{/other_user}","gists_url":"https://api.github.com/users/kevin-shanghai/gists{/gist_id}","starred_url":"https://api.github.com/users/kevin-shanghai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevin-shanghai/subscriptions","organizations_url":"https://api.github.com/users/kevin-shanghai/orgs","repos_url":"https://api.github.com/users/kevin-shanghai/repos","events_url":"https://api.github.com/users/kevin-shanghai/events{/privacy}","received_events_url":"https://api.github.com/users/kevin-shanghai/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-17T16:04:08Z","updated_at":"2015-07-17T16:04:08Z","closed_at":null,"body":"#ifndef THREAD_POOL_H__\r\n\r\n#define THREAD_POOL_H__\r\n\r\n\r\n\r\n#include \"BoundedBlockingQueue.h\"\r\n\r\n#include <thread>\r\n\r\n#include <memory>\r\n\r\n#include <atomic>\r\n\r\n#include <functional>\r\n\r\n\r\n\r\n\r\n\r\nclass ThreadPool\r\n\r\n{\r\n\r\n\ttypedef std::shared_ptr<std::thread> threadPtr;\r\n\r\n\ttypedef std::function<void(void)> threadFuncCallback;\r\n\r\n\r\n\r\npublic:\r\n\r\n\tThreadPool(int threadNum = 10) \r\n\r\n\t\t\t  : m_iThreadNum(std::thread::hardware_concurrency())\r\n\r\n\t\t\t  , m_threadFuncQueue(10)\r\n\r\n\t{\r\n\r\n\t\tstartAllThread(m_iThreadNum);\r\n\r\n\t}\r\n\r\n\r\n\r\n\t~ThreadPool()\r\n\r\n\t{\r\n\r\n\r\n\r\n\t}\r\n\r\n\r\n\r\n\tvoid AddTask(threadFuncCallback& task)\r\n\r\n\t{\r\n\r\n\t\tm_threadFuncQueue.Put(task);\r\n\r\n\t}\r\n\r\n\r\n\r\n\tvoid Stop()\r\n\r\n\t{\r\n\r\n\t\tm_brunning = false;\r\n\r\n\t\tfor (auto thd: m_threadQueue)\r\n\r\n\t\t{\r\n\r\n\t\t\tthd->join();\r\n\r\n\t\t}\r\n\r\n\t}\r\n\r\n\r\n\r\nprivate:\r\n\r\n\tvoid startAllThread(int numThreads)\r\n\r\n\t{\r\n\r\n\t\tm_brunning = true;\r\n\r\n\t\tfor (int i=0; i<numThreads; i++)\r\n\r\n\t\t{\r\n\r\n\t\t\tm_threadQueue.push_back(std::make_shared<std::thread>(&ThreadPool::RunInThread, this));\r\n\r\n\t\t}\r\n\r\n\t\t\r\n\r\n\t}\r\n\r\n\r\n\r\n\tvoid RunInThread(void)\r\n\r\n\t{\r\n\r\n\t\twhile (m_brunning)\r\n\r\n\t\t{\r\n\r\n\t\t\tauto task = m_threadFuncQueue.Take();\r\n\r\n\t\t\ttask();\r\n\r\n\t\t}\r\n\r\n\t}\r\n\r\n\r\n\r\n\r\n\r\nprivate:\r\n\r\n\tint m_iThreadNum;\r\n\r\n\tstd::deque<threadPtr> m_threadQueue;\r\n\r\n\tstd::atomic_bool m_brunning;\r\n\r\n\tBoundedBlockingQueue<threadFuncCallback> m_threadFuncQueue;\r\n\r\n\r\n\r\n};\r\n\r\n\r\n\r\n#endif"}}