kkk

 /*boost::function<void()> f;
 f = boost::bind(threadfunc);
 FuncThread thread(f);*/
 
 //thread.run();

 boost::function<void()> f;
 f = boost::bind(threadfunc);
 f();

 /*boost::thread thrd(f);*/

 /*WorkPool wp;
 wp.start();
 wp.post(thread);*/

 

#include "StdAfx.h"
#include "WorkPool.h"

boost::mutex io_mutex;

WorkPool::WorkPool(void)
{
}

WorkPool::~WorkPool(void)
{
}

void WorkPool::runInThread()
{
 try
 {
  while(1)
  {
   boost::mutex::scoped_lock lock(io_mutex);

   if(!queue.empty())
   {
    FuncThread ft = queue.front();
    queue.pop_front();

    ft.run();
   }
  }
 }
 catch (const std::exception& ex)
 {
  fprintf(stderr, "reason: %s\n", ex.what());
  abort();
 }
 catch (...)
 {
  throw; // rethrow
 }
}
void WorkPool::start()
{
 for (int i = 0; i < 10; ++i)
 {
  threads_.push_back(new boost::thread(
    boost::bind(&WorkPool::runInThread, this)));
 }
}
void WorkPool::post(FuncThread ft)
{
 boost::mutex::scoped_lock lock(io_mutex);
    queue.push_back(ft);
}

#pragma once
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <deque>
#include <boost/ptr_container/ptr_vector.hpp>
#include "FuncThread.h"
using namespace std;

class WorkPool
{
public:
 void runInThread();
 void start();
 void post(FuncThread ft);
 boost::ptr_vector<boost::thread> threads_;
 deque<FuncThread> queue;
public:
 WorkPool(void);
 ~WorkPool(void);
};

 

#include "StdAfx.h"
#include "FuncThread.h"


FuncThread::FuncThread(boost::function<void()> &Func) : PFunc(Func)
{
 
}


FuncThread::~FuncThread(void)
{
}

void FuncThread::run()
{
 PFunc();
}

 

#pragma once
#include <boost/function.hpp>
#include <boost/bind.hpp>
using namespace std;

class FuncThread
{
public:
 void run();
 boost::function<void()> PFunc;
public:
 FuncThread(boost::function<void()> &Func);
 ~FuncThread(void);
};

 

你可能感兴趣的:(kkk)