Last active
December 14, 2015 14:18
-
-
Save eahydra/5099261 to your computer and use it in GitHub Desktop.
A task thread pool Demo. Just create some thread, and then poll task.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "task_thread_pool.hpp" | |
#include <assert.h> | |
#include <ustd/thread/guard.h> | |
namespace wx { | |
namespace utility { | |
task_thread_pool_t::task_thread_pool_t() : current_task_thread_(0) {} | |
task_thread_pool_t::~task_thread_pool_t() | |
{ | |
assert(threads_.size() == 0); | |
} | |
bool task_thread_pool_t::start(int num) { | |
if (num == 0) | |
{ | |
SYSTEM_INFO system_info; | |
GetSystemInfo(&system_info); | |
num = system_info.dwNumberOfProcessors + 1; | |
} | |
std::vector<task_thread_ptr_t> threads; | |
for (int i = 0; i < num; ++i) | |
{ | |
task_thread_ptr_t task_thread_ptr(new task_thread_t); | |
if (!task_thread_ptr->begin([&](const closure_t& closure) | |
{ | |
closure.on_closure_(); | |
})) | |
{ | |
return false; | |
} | |
threads.push_back(std::move(task_thread_ptr)); | |
} | |
ustd::os::Guard<ustd::os::CriticalSection> guard(lock_); | |
threads_.swap(threads); | |
return true; | |
} | |
void task_thread_pool_t::stop() { | |
std::vector<task_thread_ptr_t> threads; | |
for (;;) | |
{ | |
ustd::os::Guard<ustd::os::CriticalSection> guard(lock_); | |
threads.swap(threads_); | |
break; | |
} | |
for (auto iter = std::begin(threads); iter != std::end(threads); ++iter) | |
{ | |
(*iter)->end(); | |
} | |
} | |
task_thread_pool_t::task_thread_t* task_thread_pool_t::get_idle_thread() | |
{ | |
ustd::os::Guard<ustd::os::CriticalSection> guard(lock_); | |
if (current_task_thread_ < threads_.size()) | |
return threads_[current_task_thread_++].get(); | |
current_task_thread_ = 0; | |
return threads_[0].get(); | |
} | |
} // namespace utility | |
} // namespace wx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TASK_THREAD_POOL_HPP_ | |
#define TASK_THREAD_POOL_HPP_ | |
#include <vector> | |
#include <memory> | |
#include <functional> | |
#include <ustd/thread/task_thread.h> | |
#include <ustd/thread/critical_section.h> | |
namespace wx { | |
namespace utility { | |
struct closure_t { | |
typedef std::function<void()> on_closure_t; | |
on_closure_t on_closure_; | |
template <typename handler_t> | |
closure_t(const handler_t& closure) : on_closure_(closure) { } | |
closure_t() { } | |
closure_t(const closure_t& right) { | |
if (&right != this) { | |
on_closure_ = right.on_closure_; | |
} | |
} | |
closure_t& operator=(const closure_t& right) { | |
if (&right != this) { | |
on_closure_ = right.on_closure_; | |
} | |
return *this; | |
} | |
}; | |
class task_thread_pool_t { | |
private: | |
typedef ustd::os::TaskThread<closure_t> task_thread_t; | |
typedef std::unique_ptr<task_thread_t> task_thread_ptr_t; | |
ustd::os::CriticalSection lock_; | |
std::vector<task_thread_ptr_t> threads_; | |
size_t current_task_thread_; | |
private: | |
task_thread_t* get_idle_thread(); | |
public: | |
task_thread_pool_t(); | |
~task_thread_pool_t(); | |
bool start(int num); | |
void stop(); | |
template <typename handler_t> | |
void post_task(const handler_t& handler) | |
{ | |
task_thread_t* task_thread = get_idle_thread(); | |
task_thread->post_task(handler); | |
} | |
}; | |
} // namespace utility | |
} // namespace wx | |
#endif // TASK_THREAD_POOL_HPP_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment