re-add accidently deleted files

This commit is contained in:
Hieu Hoang 2015-09-03 20:53:12 +01:00
parent 3373e24558
commit f0de6e1712
3 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#include "ug_thread_safe_counter.h"
#pragma once
// obsolete once intrusive_ref_counter is available everywhere
namespace Moses {
class reference_counter
{
public:
friend void intrusive_ptr_add_ref(reference_counter const* p)
{
if (p) ++p->m_refcount;
}
friend void intrusive_ptr_release(reference_counter const* p)
{
if (p && --p->m_refcount == 0)
delete p;
}
protected:
reference_counter() {}
virtual ~reference_counter() {};
private:
mutable ThreadSafeCounter m_refcount;
};
}

View File

@ -0,0 +1,31 @@
#include "ug_thread_pool.h"
namespace ug {
ThreadPool::
ThreadPool(size_t const num_workers)
: m_service(), m_busywork(new boost::asio::io_service::work(m_service))
{
m_workers.reserve(num_workers);
for (size_t i = 0; i < num_workers; ++i)
{
// boost::shared_ptr<boost::thread> t;
// t.reset(new boost::thread(boost::bind(&service_t::run, &m_service)));
boost::thread* t;
t = new boost::thread(boost::bind(&service_t::run, &m_service));
m_pool.add_thread(t);
// m_workers.push_back(t);
}
}
ThreadPool::
~ThreadPool()
{
m_busywork.reset();
m_pool.join_all();
m_service.stop();
}
}

View File

@ -0,0 +1,30 @@
// -*- mode: c++; tab-width: 2; indent-tabs-mode: nil -*-
#pragma once
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <vector>
#include <string>
namespace ug {
class ThreadPool
{
typedef boost::asio::io_service service_t;
service_t m_service;
boost::thread_group m_pool;
boost::scoped_ptr<service_t::work> m_busywork;
std::vector<boost::shared_ptr<boost::thread> > m_workers;
public:
ThreadPool(size_t const num_workers);
~ThreadPool();
template<class callable>
void add(callable& job) { m_service.post(job); }
}; // end of class declaration ThreadPool
} // end of namespace ug