Update ThreadPool
This commit is contained in:
parent
4cc2dd6276
commit
ee7ecd8d63
|
@ -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)
|
||||||
|
|
20
ThreadPool.h
20
ThreadPool.h
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user