2010-04-08 21:16:10 +04:00
|
|
|
// $Id: ThreadPool.h 3045 2010-04-05 13:07:29Z hieuhoang1972 $
|
2009-08-07 20:47:54 +04:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2009 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
|
2011-01-25 20:15:30 +03:00
|
|
|
#ifndef moses_ThreadPool_h
|
|
|
|
#define moses_ThreadPool_h
|
2009-08-07 20:47:54 +04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-03-21 19:12:52 +03:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2015-04-30 08:05:11 +03:00
|
|
|
|
2010-05-19 20:42:18 +04:00
|
|
|
#ifdef WITH_THREADS
|
2009-08-07 20:47:54 +04:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
2010-05-19 20:42:18 +04:00
|
|
|
#endif
|
2010-02-15 22:27:17 +03:00
|
|
|
|
2010-05-19 20:42:18 +04:00
|
|
|
#ifdef BOOST_HAS_PTHREADS
|
2010-02-15 22:27:17 +03:00
|
|
|
#include <pthread.h>
|
2010-02-14 03:26:34 +03:00
|
|
|
#endif
|
2009-08-07 20:47:54 +04:00
|
|
|
|
2010-02-15 22:27:17 +03:00
|
|
|
|
2011-09-07 12:08:35 +04:00
|
|
|
//#include "Util.h"
|
2009-08-07 20:47:54 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classes to implement a ThreadPool.
|
2012-06-29 02:29:46 +04:00
|
|
|
**/
|
2013-05-29 21:16:15 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2009-08-07 20:47:54 +04:00
|
|
|
|
2012-06-29 02:29:46 +04:00
|
|
|
/** A task to be executed by the ThreadPool
|
|
|
|
*/
|
2011-02-24 16:14:42 +03:00
|
|
|
class Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void Run() = 0;
|
|
|
|
virtual ~Task() {}
|
2010-05-19 20:42:18 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef WITH_THREADS
|
2009-08-07 20:47:54 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
class ThreadPool
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
public:
|
2011-02-24 16:14:42 +03:00
|
|
|
/**
|
2012-02-01 10:33:26 +04:00
|
|
|
* Construct a thread pool of a fixed size.
|
|
|
|
**/
|
|
|
|
explicit ThreadPool(size_t numThreads);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2012-02-01 10:33:26 +04:00
|
|
|
~ThreadPool() {
|
|
|
|
Stop();
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a job to the threadpool.
|
|
|
|
**/
|
2015-03-21 19:12:52 +03:00
|
|
|
void Submit(boost::shared_ptr<Task> task);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
/**
|
2012-02-01 10:33:26 +04:00
|
|
|
* Wait until all queued jobs have completed, and shut down
|
|
|
|
* the ThreadPool.
|
|
|
|
**/
|
2011-02-24 16:14:42 +03:00
|
|
|
void Stop(bool processRemainingJobs = false);
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
/**
|
|
|
|
* Set maximum number of queued threads (otherwise Submit blocks)
|
|
|
|
**/
|
2013-05-29 21:16:15 +04:00
|
|
|
void SetQueueLimit( size_t limit ) {
|
|
|
|
m_queueLimit = limit;
|
|
|
|
}
|
2012-04-17 08:54:48 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
private:
|
|
|
|
/**
|
2012-02-01 10:33:26 +04:00
|
|
|
* The main loop executed by each thread.
|
|
|
|
**/
|
2011-02-24 16:14:42 +03:00
|
|
|
void Execute();
|
|
|
|
|
2015-03-21 19:12:52 +03:00
|
|
|
std::queue<boost::shared_ptr<Task> > m_tasks;
|
2011-02-24 16:14:42 +03:00
|
|
|
boost::thread_group m_threads;
|
|
|
|
boost::mutex m_mutex;
|
|
|
|
boost::condition_variable m_threadNeeded;
|
|
|
|
boost::condition_variable m_threadAvailable;
|
|
|
|
bool m_stopped;
|
|
|
|
bool m_stopping;
|
2012-04-17 08:54:48 +04:00
|
|
|
size_t m_queueLimit;
|
2009-08-07 20:47:54 +04:00
|
|
|
};
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
class TestTask : public Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestTask(int id) : m_id(id) {}
|
2012-02-01 10:33:26 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
virtual void Run() {
|
2010-05-19 20:42:18 +04:00
|
|
|
#ifdef BOOST_HAS_PTHREADS
|
2011-02-24 16:14:42 +03:00
|
|
|
pthread_t tid = pthread_self();
|
2009-11-13 13:13:55 +03:00
|
|
|
#else
|
2013-05-29 21:16:15 +04:00
|
|
|
typedef void * pthread_t;
|
2011-02-24 16:14:42 +03:00
|
|
|
pthread_t tid = 0;
|
2009-11-13 13:13:55 +03:00
|
|
|
#endif
|
2011-02-24 16:14:42 +03:00
|
|
|
std::cerr << "Executing " << m_id << " in thread id " << tid << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~TestTask() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_id;
|
2009-08-07 20:47:54 +04:00
|
|
|
};
|
|
|
|
|
2010-05-19 20:42:18 +04:00
|
|
|
#endif //WITH_THREADS
|
2009-08-07 20:47:54 +04:00
|
|
|
|
2012-02-01 10:33:26 +04:00
|
|
|
} // namespace Moses
|
|
|
|
#endif // moses_ThreadPool_h
|