Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CMakeLists.txt #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
project(thread_pool)
cmake_minimum_required(VERSION 2.6)
include_directories(./)
add_definitions("-Wall -std=c++11 -g")

aux_source_directory(. SRC)
add_executable(theadpool ${SRC})
target_link_libraries(theadpool -pthread)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ThreadPool
==========
C++11 简易线程池测试

A simple C++11 Thread Pool implementation.

Basic usage:
```c++
Expand Down
56 changes: 31 additions & 25 deletions ThreadPool.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,34 @@
#include <stdexcept>

class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;

// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;

private:
//工作线程
std::vector< std::thread > workers;
//任务队列
std::queue< std::function<void()> > tasks;

//线程同步
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;

public:
ThreadPool(size_t);
~ThreadPool();

//添加任务
template<typename F, typename... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
};

// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
inline ThreadPool::ThreadPool(size_t threads):stop(false)
{
for(size_t i = 0;i<threads;++i)
for(size_t i = 0;i<threads;i++)
workers.emplace_back(
[this]
[=]()
{
for(;;)
{
Expand All @@ -48,23 +51,25 @@ inline ThreadPool::ThreadPool(size_t threads)
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty())
return;
//抽取任务
task = std::move(this->tasks.front());
this->tasks.pop();
}

//执行任务
task();
}
}
);
}

// add new work item to the pool
template<class F, class... Args>
template<typename F, typename... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;

//封装任务为void() 类型 ,方便执行
auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
Expand All @@ -76,8 +81,9 @@ auto ThreadPool::enqueue(F&& f, Args&&... args)
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");

tasks.emplace([task](){ (*task)(); });

//添加任务
tasks.emplace([=](){ (*task)(); });
}
condition.notify_one();
return res;
Expand All @@ -91,7 +97,7 @@ inline ThreadPool::~ThreadPool()
stop = true;
}
condition.notify_all();
for(std::thread &worker: workers)
for(auto &worker: workers)
worker.join();
}

Expand Down
Loading