Lucene++ - a full-featured, c++ search engine
API Documentation

Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1
2// Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3// Distributable under the terms of either the Apache License (Version 2.0)
4// or the GNU Lesser General Public License.
6
7#ifndef THREADPOOL_H
8#define THREADPOOL_H
9
10#include <boost/asio.hpp>
11#include <boost/any.hpp>
12#include <boost/thread/thread.hpp>
13#include "LuceneObject.h"
14
15 namespace Lucene {
16
17
18 typedef boost::asio::io_context io_context_t;
19 typedef boost::asio::executor_work_guard<io_context_t::executor_type> work_t;
20
24 class Future : public LuceneObject {
25 public:
26 virtual ~Future();
27
28 protected:
29 boost::any value;
30
31 public:
32 void set(const boost::any& value) {
33 SyncLock syncLock(this);
34 this->value = value;
35 }
36
37 template <typename TYPE>
38 TYPE get() {
39 SyncLock syncLock(this);
40 while (value.empty()) {
41 wait(10);
42 }
43 return value.empty() ? TYPE() : boost::any_cast<TYPE>(value);
44 }
45};
46
48 class ThreadPool : public LuceneObject {
49 public:
50 ThreadPool();
51 virtual ~ThreadPool();
52
53 LUCENE_CLASS(ThreadPool);
54
55 protected:
56 io_context_t io_context;
57 work_t work;
58 boost::thread_group threadGroup;
59
60 static const int32_t THREADPOOL_SIZE;
61
62 public:
64 static ThreadPoolPtr getInstance();
65
66 template <typename FUNC>
67 FuturePtr scheduleTask(FUNC func) {
68 FuturePtr future(newInstance<Future>());
69 boost::asio::post(io_context, boost::bind(&ThreadPool::execute<FUNC>, this, func, future));
70 return future;
71 }
72
73 protected:
74 // this will be executed when one of the threads is available
75 template <typename FUNC>
76 void execute(FUNC func, const FuturePtr& future) {
77 future->set(func());
78 future->notifyAll();
79 }
80};
81
82}
83
84#endif
LuceneObject.h
LUCENE_CLASS
#define LUCENE_CLASS(Name)
Definition LuceneObject.h:24
Lucene::Future
A Future represents the result of an asynchronous computation. Methods are provided to check if the c...
Definition ThreadPool.h:24
Lucene::Future::value
boost::any value
Definition ThreadPool.h:29
Lucene::Future::~Future
virtual ~Future()
Lucene::Future::get
TYPE get()
Definition ThreadPool.h:38
Lucene::Future::set
void set(const boost::any &value)
Definition ThreadPool.h:32
Lucene::LuceneObject
Base class for all Lucene classes.
Definition LuceneObject.h:31
Lucene::LuceneSync::wait
virtual void wait(int32_t timeout=0)
Wait for signal using an optional timeout.
Lucene::SyncLock
Utility class to support scope locking.
Definition Synchronize.h:46
Lucene::ThreadPool
Utility class to handle a pool of threads.
Definition ThreadPool.h:48
Lucene::ThreadPool::scheduleTask
FuturePtr scheduleTask(FUNC func)
Definition ThreadPool.h:67
Lucene::ThreadPool::~ThreadPool
virtual ~ThreadPool()
Lucene::ThreadPool::threadGroup
boost::thread_group threadGroup
Definition ThreadPool.h:58
Lucene::ThreadPool::execute
void execute(FUNC func, const FuturePtr &future)
Definition ThreadPool.h:76
Lucene::ThreadPool::getInstance
static ThreadPoolPtr getInstance()
Get singleton thread pool instance.
Lucene::ThreadPool::ThreadPool
ThreadPool()
Lucene::ThreadPool::work
work_t work
Definition ThreadPool.h:57
Lucene::ThreadPool::io_context
io_context_t io_context
Definition ThreadPool.h:56
Lucene::ThreadPool::THREADPOOL_SIZE
static const int32_t THREADPOOL_SIZE
Definition ThreadPool.h:60
Lucene
Definition AbstractAllTermDocs.h:12
Lucene::FuturePtr
boost::shared_ptr< Future > FuturePtr
Definition LuceneTypes.h:530
Lucene::ThreadPoolPtr
boost::shared_ptr< ThreadPool > ThreadPoolPtr
Definition LuceneTypes.h:553
Lucene::work_t
boost::asio::executor_work_guard< io_context_t::executor_type > work_t
Definition ThreadPool.h:19
Lucene::io_context_t
boost::asio::io_context io_context_t
Definition ThreadPool.h:18

clucene.sourceforge.net