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


ThreadPool.h
Go to the documentation of this file.
1 // 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 // https://www.osdyson.org/issues/196
11 #undef _BSD_SOURCE
12 
13 #include <boost/asio.hpp>
14 #include <boost/any.hpp>
15 #include <boost/thread/thread.hpp>
16 #include "LuceneObject.h"
17 
18 namespace Lucene {
19 
20 typedef boost::shared_ptr<boost::asio::io_service::work> workPtr;
21 
25 class Future : public LuceneObject {
26 public:
27  virtual ~Future();
28 
29 protected:
30  boost::any value;
31 
32 public:
33  void set(const boost::any& value) {
34  SyncLock syncLock(this);
35  this->value = value;
36  }
37 
38  template <typename TYPE>
39  TYPE get() {
40  SyncLock syncLock(this);
41  while (value.empty()) {
42  wait(10);
43  }
44  return value.empty() ? TYPE() : boost::any_cast<TYPE>(value);
45  }
46 };
47 
49 class ThreadPool : public LuceneObject {
50 public:
51  ThreadPool();
52  virtual ~ThreadPool();
53 
55 
56 protected:
57  boost::asio::io_service io_service;
58  workPtr work;
59  boost::thread_group threadGroup;
60 
61  static const int32_t THREADPOOL_SIZE;
62 
63 public:
65  static ThreadPoolPtr getInstance();
66 
67  template <typename FUNC>
68  FuturePtr scheduleTask(FUNC func) {
69  FuturePtr future(newInstance<Future>());
70  io_service.post(boost::bind(&ThreadPool::execute<FUNC>, this, func, future));
71  return future;
72  }
73 
74 protected:
75  // this will be executed when one of the threads is available
76  template <typename FUNC>
77  void execute(FUNC func, const FuturePtr& future) {
78  future->set(func());
79  future->notifyAll();
80  }
81 };
82 
83 }
84 
85 #endif
boost::shared_ptr< boost::asio::io_service::work > workPtr
Definition: ThreadPool.h:20
boost::shared_ptr< ThreadPool > ThreadPoolPtr
Definition: LuceneTypes.h:553
static ThreadPoolPtr getInstance()
Get singleton thread pool instance.
virtual ~ThreadPool()
workPtr work
Definition: ThreadPool.h:58
Utility class to handle a pool of threads.
Definition: ThreadPool.h:49
void execute(FUNC func, const FuturePtr &future)
Definition: ThreadPool.h:77
boost::any value
Definition: ThreadPool.h:30
A Future represents the result of an asynchronous computation. Methods are provided to check if the c...
Definition: ThreadPool.h:25
virtual void wait(int32_t timeout=0)
Wait for signal using an optional timeout.
void set(const boost::any &value)
Definition: ThreadPool.h:33
Utility class to support scope locking.
Definition: Synchronize.h:44
boost::thread_group threadGroup
Definition: ThreadPool.h:59
Base class for all Lucene classes.
Definition: LuceneObject.h:31
#define LUCENE_CLASS(Name)
Definition: LuceneObject.h:24
Definition: AbstractAllTermDocs.h:12
virtual ~Future()
static const int32_t THREADPOOL_SIZE
Definition: ThreadPool.h:61
boost::shared_ptr< Future > FuturePtr
Definition: LuceneTypes.h:530
boost::asio::io_service io_service
Definition: ThreadPool.h:54
FuturePtr scheduleTask(FUNC func)
Definition: ThreadPool.h:68

clucene.sourceforge.net