Skip to content

Migrate to boost::asio::io_context #210

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

Merged
Merged
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
10 changes: 6 additions & 4 deletions include/lucene++/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

namespace Lucene {

typedef boost::shared_ptr<boost::asio::io_service::work> workPtr;

typedef boost::asio::io_context io_context_t;
typedef boost::asio::executor_work_guard<io_context_t::executor_type> work_t;

/// A Future represents the result of an asynchronous computation. Methods are provided to check if the computation
/// is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be
Expand Down Expand Up @@ -51,8 +53,8 @@ class ThreadPool : public LuceneObject {
LUCENE_CLASS(ThreadPool);

protected:
boost::asio::io_service io_service;
workPtr work;
io_context_t io_context;
work_t work;
boost::thread_group threadGroup;

static const int32_t THREADPOOL_SIZE;
Expand All @@ -64,7 +66,7 @@ class ThreadPool : public LuceneObject {
template <typename FUNC>
FuturePtr scheduleTask(FUNC func) {
FuturePtr future(newInstance<Future>());
io_service.post(boost::bind(&ThreadPool::execute<FUNC>, this, func, future));
boost::asio::post(io_context, boost::bind(&ThreadPool::execute<FUNC>, this, func, future));
return future;
}

Expand Down
9 changes: 5 additions & 4 deletions src/core/util/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ Future::~Future() {

const int32_t ThreadPool::THREADPOOL_SIZE = 5;

ThreadPool::ThreadPool() {
work.reset(new boost::asio::io_service::work(io_service));
ThreadPool::ThreadPool()
:
work(boost::asio::make_work_guard(io_context))
{
for (int32_t i = 0; i < THREADPOOL_SIZE; ++i) {
threadGroup.create_thread(boost::bind(&boost::asio::io_service::run, &io_service));
threadGroup.create_thread(boost::bind(&boost::asio::io_context::run, &io_context));
}
}

ThreadPool::~ThreadPool() {
work.reset(); // stop all threads
threadGroup.join_all(); // wait for all competition
}

Expand Down