7 #ifndef MT4CPP_SHEDULER_HPP
8 #define MT4CPP_SHEDULER_HPP
12 #include <boost/noncopyable.hpp>
13 #include <boost/scoped_ptr.hpp>
14 #include <boost/thread.hpp>
15 #include <boost/bind.hpp>
31 boost::mutex::scoped_lock lock(m_);
32 while(queue_.empty()) {
39 PCommand f = queue_.front();
48 boost::mutex::scoped_lock lock(m_);
51 CommandID command_id = ++currentCommandId_;
52 cmd->setId(command_id);
53 cmd->setState(CommandDesc::QUEUED);
60 boost::mutex::scoped_lock lock(m_);
65 boost::mutex::scoped_lock lock(m_);
70 long generateCommandId() {
71 boost::mutex::scoped_lock lock(m_);
72 return ++currentCommandId_;
75 std::queue<PCommand> queue_;
76 mutable boost::mutex m_;
78 boost::condition_variable cond;
83 CommandID currentCommandId_;
119 typedef std::list<boost::thread*> Threads;
121 mutable boost::mutex m_;
142 void threadFunction(CommandQueue& queue) {
143 while(!queue.getQuitFlag()) {
144 PCommand cmd = queue.read();
154 inline Scheduler::Scheduler(
int num_threads) : num_(num_threads) {
156 for(
int i = 0; i < num_; ++i) {
157 boost::lock_guard<boost::mutex> guard(m_);
158 boost::thread* new_thread_ptr =
new boost::thread(boost::bind(threadFunction, boost::ref(queue_)));
160 threads_.push_back(new_thread_ptr);
167 inline Scheduler::~Scheduler() {
169 boost::lock_guard<boost::mutex> guard(m_);
170 for(Threads::iterator it=threads_.begin(), end=threads_.end(); it!=end; ++it) {
179 return queue_.
write(cmd);
186 FinishObserver() :done(false) {}
188 virtual void notifyProgress(
const Command&,
double) {}
189 virtual void notifyStep(
const Command&) {}
191 if(s == CommandDesc::DONE || s == CommandDesc::INTERRUPTED || s == CommandDesc::EXCEPTION) {
192 boost::mutex::scoped_lock lock(mut);
198 boost::condition_variable cond;
210 FinishObserver* obs =
new FinishObserver();
211 cmd->attach( PCommandObserver( obs ) );
215 boost::mutex::scoped_lock lock(obs->mut);
216 while( ! obs->done ) {
217 obs->cond.wait( lock );
226 boost::lock_guard<boost::mutex> guard(m_);
227 for(Threads::iterator it=threads_.begin(),end=threads_.end(); it!=end; ++it) {
228 if( (*it)->joinable() )
242 #endif //MT4CPP_SHEDULER_HPP
The command module (Command base class, progress, command description).
State
available states of Command NONE - command created, but not put in activation queue QUEUED - command ...
Definition: Command.hpp:37
Definition: Scheduler.hpp:90
Definition: Scheduler.hpp:22
CommandID executeSynchronously(PCommand cmd)
Definition: Scheduler.hpp:209
bool getQuitFlag() const
Definition: Scheduler.hpp:59
CommandID write(PCommand cmd)
Definition: Scheduler.hpp:45
CommandQueue()
Definition: Scheduler.hpp:25
CommandID executeAsynchronously(PCommand cmd)
Definition: Scheduler.hpp:178
void join()
Definition: Scheduler.hpp:225
Definition: Command.hpp:21
PCommand read()
Definition: Scheduler.hpp:30
void finishAndJoin()
Definition: Scheduler.hpp:234
Definition: Command.hpp:51
void setQuitFlag()
Definition: Scheduler.hpp:64