-
-
Save rightfold/b8db7d36fbf83239b1a2 to your computer and use it in GitHub Desktop.
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 <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(); | |
} |
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
#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