1. 程式人生 > >boost之執行緒池使用例項

boost之執行緒池使用例項

#pragma once

#ifndef THREAD_POOL_H
#define THREAD_POOL_H

#include "boost_comm.h"

using namespace std;

//using namespace boost::executors;
namespace seemmo
{
#ifdef  ENABLE_BOOST_THREAD
	class thread_pool
	{
	public:
		explicit thread_pool(int thread_nums = 24);
		~thread_pool();
	protected:
		static thread_pool *instance()
		{
			static thread_pool inst(48);
			return &inst;
		}
	private:
		basic_thread_pool *tp;
		boost::mutex mtx_;
	public:
		static void post_task(void(*work)());
	};
#endif //  ENABLE_BOOST_THREAD
}


#endif // THREAD_POOL_H

#include "thread_pool.h"
#include "picture_search.h"

namespace seemmo
{
#ifdef ENABLE_BOOST_THREAD
	thread_pool::thread_pool(int init_nums)
	{
		tp = new basic_thread_pool(init_nums);
	}

	thread_pool::~thread_pool()
	{
		delete tp;
	}
	void thread_pool::post_task(void(*work)())
	{
		instance()->tp->submit(work);
	}
#endif
}