2010-04-08 21:16:10 +04:00
|
|
|
// $Id: ThreadPool.cpp 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
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "ThreadPool.h"
|
|
|
|
|
2010-05-19 20:42:18 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
|
2009-08-07 20:47:54 +04:00
|
|
|
using namespace std;
|
|
|
|
using namespace Moses;
|
|
|
|
|
2011-03-11 19:28:36 +03:00
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
|
|
|
ThreadPool::ThreadPool( size_t numThreads )
|
2012-04-17 08:54:48 +04:00
|
|
|
: m_stopped(false), m_stopping(false), m_queueLimit(0)
|
2009-08-07 20:47:54 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
for (size_t i = 0; i < numThreads; ++i) {
|
|
|
|
m_threads.create_thread(boost::bind(&ThreadPool::Execute,this));
|
|
|
|
}
|
2009-08-07 20:47:54 +04:00
|
|
|
}
|
|
|
|
|
2011-03-11 19:28:36 +03:00
|
|
|
void ThreadPool::Execute()
|
2009-08-07 20:47:54 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
do {
|
|
|
|
Task* task = NULL;
|
|
|
|
{
|
|
|
|
// Find a job to perform
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
if (m_tasks.empty() && !m_stopped) {
|
|
|
|
m_threadNeeded.wait(lock);
|
|
|
|
}
|
|
|
|
if (!m_stopped && !m_tasks.empty()) {
|
|
|
|
task = m_tasks.front();
|
|
|
|
m_tasks.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Execute job
|
|
|
|
if (task) {
|
|
|
|
task->Run();
|
2011-09-07 12:08:35 +04:00
|
|
|
if (task->DeleteAfterExecution()) {
|
|
|
|
delete task;
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
m_threadAvailable.notify_all();
|
|
|
|
} while (!m_stopped);
|
2009-08-07 20:47:54 +04:00
|
|
|
}
|
|
|
|
|
2011-03-11 19:28:36 +03:00
|
|
|
void ThreadPool::Submit( Task* task )
|
2009-08-07 20:47:54 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
if (m_stopping) {
|
|
|
|
throw runtime_error("ThreadPool stopping - unable to accept new jobs");
|
|
|
|
}
|
2012-04-18 02:41:45 +04:00
|
|
|
while (m_queueLimit > 0 && m_tasks.size() >= m_queueLimit) {
|
2012-04-17 08:54:48 +04:00
|
|
|
m_threadAvailable.wait(lock);
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
m_tasks.push(task);
|
|
|
|
m_threadNeeded.notify_all();
|
2009-08-07 20:47:54 +04:00
|
|
|
}
|
|
|
|
|
2011-03-11 19:28:36 +03:00
|
|
|
void ThreadPool::Stop(bool processRemainingJobs)
|
2009-08-07 20:47:54 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
{
|
|
|
|
//prevent more jobs from being added to the queue
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
if (m_stopped) return;
|
|
|
|
m_stopping = true;
|
|
|
|
}
|
|
|
|
if (processRemainingJobs) {
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
//wait for queue to drain.
|
|
|
|
while (!m_tasks.empty() && !m_stopped) {
|
|
|
|
m_threadAvailable.wait(lock);
|
2009-08-07 20:47:54 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
//tell all threads to stop
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
m_stopped = true;
|
|
|
|
}
|
|
|
|
m_threadNeeded.notify_all();
|
|
|
|
|
|
|
|
m_threads.join_all();
|
2009-08-07 20:47:54 +04:00
|
|
|
}
|
2011-03-11 19:28:36 +03:00
|
|
|
|
|
|
|
}
|
2010-05-19 20:42:18 +04:00
|
|
|
#endif //WITH_THREADS
|
2011-03-11 19:28:36 +03:00
|
|
|
|