Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created February 3, 2015 22:07
Show Gist options
  • Save rightfold/b8db7d36fbf83239b1a2 to your computer and use it in GitHub Desktop.
Save rightfold/b8db7d36fbf83239b1a2 to your computer and use it in GitHub Desktop.
#include <cstddef>
#include <thread>
#include "threadPool.hpp"
agluj::ThreadPool::ThreadPool()
: ThreadPool(std::thread::hardware_concurrency()) { }
agluj::ThreadPool::ThreadPool(std::size_t threadCount)
: work(ioService) {
if (threadCount == 0) {
threadCount = 1;
}
for (decltype(threadCount) i = 0; i < threadCount; ++i) {
threads.create_thread([=] {
ioService.run();
});
}
}
agluj::ThreadPool::~ThreadPool() {
ioService.stop();
threads.join_all();
}
#pragma once
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <cstddef>
namespace agluj {
class ThreadPool {
public:
ThreadPool();
explicit ThreadPool(std::size_t threadCount);
~ThreadPool();
template<typename F>
void post(F function) {
ioService.post(std::move(function));
}
private:
boost::thread_group threads;
boost::asio::io_service ioService;
boost::asio::io_service::work work;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment