Update ThreadPool

This commit is contained in:
Kirigaya Kazuto 2017-11-09 14:50:13 +08:00
parent 4cc2dd6276
commit ee7ecd8d63
2 changed files with 19 additions and 14 deletions

View File

@ -1,7 +1,18 @@
#include "thread_pool.h" #include "thread_pool.h"
struct ThreadWorkerData
{
std::function<void()> func;
std::mutex m;
std::condition_variable cond;
bool started;
bool finished;
bool running;
};
using namespace std; using namespace std;
void _global_thread_worker_main(ThreadWorkerData* ptdd) static void _global_thread_worker_main(ThreadWorkerData* ptdd)
{ {
unique_lock<mutex> ulk(ptdd->m); unique_lock<mutex> ulk(ptdd->m);
while(ptdd->running) while(ptdd->running)

View File

@ -5,26 +5,20 @@
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
struct ThreadWorkerData struct ThreadWorkerData;
{
std::function<void()> func;
std::mutex m;
std::condition_variable cond;
bool started;
bool finished;
bool running;
};
void _global_thread_worker_main(ThreadWorkerData* ptdd);
class ThreadPool class ThreadPool
{ {
public: public:
ThreadPool(int n); ThreadPool(int n);
/// NonCopyable, NonMovable.
ThreadPool(const ThreadPool&)=delete;
ThreadPool& operator = (const ThreadPool&)=delete;
ThreadPool(ThreadPool&&)=delete;
ThreadPool& operator = (ThreadPool&&)=delete;
~ThreadPool(); ~ThreadPool();
int start(const std::function<void()>& func); int start(const std::function<void()>&);
private: private:
std::vector<ThreadWorkerData*> dvec; std::vector<ThreadWorkerData*> dvec;
std::vector<std::thread*> tvec; std::vector<std::thread*> tvec;