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 typedef boost::shared_ptr<boost::asio::io_service::work> workPtr;
18
22 class Future : public LuceneObject {
23 public:
24 virtual ~Future();
25
26 protected:
27 boost::any value;
28
29 public:
30 void set(const boost::any& value) {
31 SyncLock syncLock(this);
32 this->value = value;
33 }
34
35 template <typename TYPE>
36 TYPE get() {
37 SyncLock syncLock(this);
38 while (value.empty()) {
39 wait(10);
40 }
41 return value.empty() ? TYPE() : boost::any_cast<TYPE>(value);
42 }
43};
44
46 class ThreadPool : public LuceneObject {
47 public:
48 ThreadPool();
49 virtual ~ThreadPool();
50
51 LUCENE_CLASS(ThreadPool);
52
53 protected:
54 boost::asio::io_service io_service;
55 workPtr work;
56 boost::thread_group threadGroup;
57
58 static const int32_t THREADPOOL_SIZE;
59
60 public:
62 static ThreadPoolPtr getInstance();
63
64 template <typename FUNC>
65 FuturePtr scheduleTask(FUNC func) {
66 FuturePtr future(newInstance<Future>());
67 io_service.post(boost::bind(&ThreadPool::execute<FUNC>, this, func, future));
68 return future;
69 }
70
71 protected:
72 // this will be executed when one of the threads is available
73 template <typename FUNC>
74 void execute(FUNC func, const FuturePtr& future) {
75 future->set(func());
76 future->notifyAll();
77 }
78};
79
80}
81
82#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:22
Lucene::Future::value
boost::any value
Definition ThreadPool.h:27
Lucene::Future::~Future
virtual ~Future()
Lucene::Future::get
TYPE get()
Definition ThreadPool.h:36
Lucene::Future::set
void set(const boost::any &value)
Definition ThreadPool.h:30
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:46
Lucene::ThreadPool::scheduleTask
FuturePtr scheduleTask(FUNC func)
Definition ThreadPool.h:65
Lucene::ThreadPool::~ThreadPool
virtual ~ThreadPool()
Lucene::ThreadPool::io_service
boost::asio::io_service io_service
Definition ThreadPool.h:54
Lucene::ThreadPool::threadGroup
boost::thread_group threadGroup
Definition ThreadPool.h:56
Lucene::ThreadPool::execute
void execute(FUNC func, const FuturePtr &future)
Definition ThreadPool.h:74
Lucene::ThreadPool::getInstance
static ThreadPoolPtr getInstance()
Get singleton thread pool instance.
Lucene::ThreadPool::ThreadPool
ThreadPool()
Lucene::ThreadPool::work
workPtr work
Definition ThreadPool.h:55
Lucene::ThreadPool::THREADPOOL_SIZE
static const int32_t THREADPOOL_SIZE
Definition ThreadPool.h:58
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::workPtr
boost::shared_ptr< boost::asio::io_service::work > workPtr
Definition ThreadPool.h:17

clucene.sourceforge.net