10 #include <boost/bind.hpp> 11 #include <boost/shared_ptr.hpp> 12 #include <boost/thread/condition.hpp> 13 #include <boost/thread/thread.hpp> 21 template <
class _T,
class _P>
27 _T* pThis =
reinterpret_cast<_T*
>(
this);
28 return pThis->runTask(_param);
32 template <
class _T,
class _P>
39 CTask(task_t& _task, _P& _param)
46 return m_task.run(m_taskParam);
54 template <
class _T,
class _P>
58 typedef std::queue<task_t*> taskqueue_t;
64 , m_successfulTasks(0)
68 for (
size_t i = 0; i < _threadsCount; ++i)
79 boost::mutex::scoped_lock lock(m_mutex);
80 task_t* task =
new task_t(_task, _param);
82 m_threadNeeded.notify_all();
93 boost::mutex::scoped_lock lock(m_mutex);
94 if (m_tasks.empty() && !m_stopped)
96 m_threadNeeded.wait(lock);
98 if (!m_stopped && !m_tasks.empty())
100 task = m_tasks.front();
109 boost::mutex::scoped_lock lock(m_mutex);
115 m_threadAvailable.notify_all();
116 }
while (!m_stopped);
118 void stop(
bool processRemainingJobs =
false)
122 boost::mutex::scoped_lock lock(m_mutex);
127 if (processRemainingJobs)
129 boost::mutex::scoped_lock lock(m_mutex);
131 while (!m_tasks.empty() && !m_stopped)
133 m_threadAvailable.wait(lock);
138 boost::mutex::scoped_lock lock(m_mutex);
141 m_threadNeeded.notify_all();
143 m_threads.join_all();
151 return m_successfulTasks;
155 boost::thread_group m_threads;
157 boost::mutex m_mutex;
158 boost::condition m_threadNeeded;
159 boost::condition m_threadAvailable;
162 size_t m_successfulTasks;
bool run(_P &_param)
Definition: threadPool.h:25
Definition: threadPool.h:33
size_t successfulTasks() const
Definition: threadPool.h:149
CTaskImp< _T, _P > task_t
Definition: threadPool.h:36
CThreadPool(size_t _threadsCount)
Definition: threadPool.h:61
Definition: dds-agent/src/AgentConnectionManager.h:18
~CThreadPool()
Definition: threadPool.h:72
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
void execute()
Definition: threadPool.h:85
Definition: threadPool.h:55
void pushTask(typename CTask< _T, _P >::task_t &_task, _P _param)
Definition: threadPool.h:77
CTask(task_t &_task, _P &_param)
Definition: threadPool.h:39
void stop(bool processRemainingJobs=false)
Definition: threadPool.h:118
bool run()
Definition: threadPool.h:44
Definition: threadPool.h:22
size_t tasksCount() const
Definition: threadPool.h:145