相关疑难解决方法(0)

使用boost asio的线程池

我正在尝试使用boost :: asio创建一个有限的线程池类.但是我被困在某一点可以帮助我.

唯一的问题是我应该减少反击的地方?

代码无法按预期工作.

问题是我不知道我的线程何时完成执行以及我将如何知道它已经返回池中

#include <boost/asio.hpp>
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/thread/mutex.hpp>
#include <stack>

using namespace std;
using namespace boost;

class ThreadPool
{
    static int count;
    int NoOfThread;
    thread_group grp;
    mutex mutex_;
    asio::io_service io_service;
    int counter;
    stack<thread*> thStk ;

public:
    ThreadPool(int num)
    {   
        NoOfThread = num;
        counter = 0;
        mutex::scoped_lock lock(mutex_);

        if(count == 0)
            count++;
        else
            return;

        for(int i=0 ; i<num ; ++i)
        {
            thStk.push(grp.create_thread(boost::bind(&asio::io_service::run, &io_service)));
        }
    }
    ~ThreadPool()
    {
        io_service.stop();
        grp.join_all();
    }

    thread* getThread()
    { …
Run Code Online (Sandbox Code Playgroud)

c++ boost-asio threadpool

20
推荐指数
1
解决办法
2万
查看次数

标签 统计

boost-asio ×1

c++ ×1

threadpool ×1