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"
struct ThreadWorkerData
{
std::function<void()> func;
std::mutex m;
std::condition_variable cond;
bool started;
bool finished;
bool running;
};
using namespace std;
void _global_thread_worker_main(ThreadWorkerData* ptdd)
static void _global_thread_worker_main(ThreadWorkerData* ptdd)
{
unique_lock<mutex> ulk(ptdd->m);
while(ptdd->running)

View File

@ -5,26 +5,20 @@
#include <mutex>
#include <condition_variable>
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);
struct ThreadWorkerData;
class ThreadPool
{
public:
ThreadPool(int n);
/// NonCopyable, NonMovable.
ThreadPool(const ThreadPool&)=delete;
ThreadPool& operator = (const ThreadPool&)=delete;
ThreadPool(ThreadPool&&)=delete;
ThreadPool& operator = (ThreadPool&&)=delete;
~ThreadPool();
int start(const std::function<void()>& func);
int start(const std::function<void()>&);
private:
std::vector<ThreadWorkerData*> dvec;
std::vector<std::thread*> tvec;