提升线程和shared_ptrs

时间:2018-01-24 19:32:38

标签: c++ multithreading boost c++14 shared-ptr

我正在与朋友一起开展一个侧面项目,并正在使用Boost线程和asio构建一个线程池。我已经启动并运行了一个基本池,但希望能够从池中删除线程。下面的类保留了std::shared_ptr<boost::thread>的向量。

我遇到的问题是当我运行一个简单的测试时,程序永远不会退出。它似乎到达终点(见输出)但永远不会终止。我有一种感觉,它与仍然活着的线程有关,并试图杀死它们但无济于事。我做了一些关于清除共享指针向量的搜索,看起来你真的不应该这样做。同样,对thread_group的{​​{1}}调用应该加入并结束所有线程,至少我是这么认为的。

我仍在完全学习提升线程,asio和shared_ptr,因此我们将非常感谢任何帮助或建议。

编译:`

join_all()

类别:

g++ -std=c++14 main.cpp -lboost_system -lboost_thread -lpthread

主要

  class experimentalPool {
      private:
        boost::asio::io_service ioService;
        boost::asio::io_service::work work;
        boost::thread_group threads;
        std::vector<std::shared_ptr<boost::thread>> workers;
        std::size_t poolsize;
      public:
        experimentalPool(): work( ioService ) {
            poolsize = boost::thread::hardware_concurrency();
            for (size_t i = 0; i < poolsize; i++) {
                std::shared_ptr<boost::thread> t1(new boost::thread(
                    boost::bind(&boost::asio::io_service::run, &ioService)));
                threads.add_thread(t1.get());
                workers.push_back(std::move(t1));
            }
        }

        experimentalPool( std::size_t psize )
          : work( ioService ),
            poolsize( psize )
        {
            for (size_t i = 0; i < poolsize; i++) {
                std::shared_ptr<boost::thread> t1(new boost::thread(
                    boost::bind(&boost::asio::io_service::run, &ioService)));
                threads.add_thread(t1.get());
                workers.push_back(std::move(t1));
            }
        }

        template <typename F, typename... Args>
        void add_work(F f, Args... args){
            ioService.post(boost::bind(f, args...));
        };

        ~experimentalPool(){
          ioService.stop();
          try {
            std::cout << "here" << "\n";
            threads.join_all();
            //threads.interrupt_all();
            std::cout << "here" << "\n";
            //for (size_t i = 0; i < workers.size(); i++) {
              //(*workers[i]).interrupt();
            //}
          }
          catch ( const std::exception& ) {std::cout << "Caught Exception" << "\n";}
        }

        std::size_t size() const { return poolsize;}
    };

输出:

void add1(int& num){
    std::cout << ++num << std::endl;
}

int main(){

    int temp = 5;

    experimentalPool xpool(2);
    std::cout << "Xpool " << pool2.size() << "\n";
    xpool.add_work(add1, temp);

    std::this_thread::sleep_for (std::chrono::seconds(1));
    std::cout << "End" << std::endl;
    return 0;
}

2 个答案:

答案 0 :(得分:1)

说实话,我看了几分钟的代码并且越来越关注。

为什么两个集合中的线程(thread_group和向量)?为什么线程在另一个集合中拥有?为什么工作对象是永久性的?你为什么要砍掉服务而不是让游泳池耗尽呢?

为什么池化冗余存储?

为什么构造函数99%重复?

为什么add1采用可变引用,但绑定是按值?

简化

<强> Live On Coliru

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <thread>

namespace Experimental { 
    namespace ba = boost::asio;

    class Pool {
      private:
        ba::io_service ioService;
        boost::thread_group threads;
        boost::optional<ba::io_service::work> work { ioService };

      public:
        Pool(std::size_t psize = boost::thread::hardware_concurrency()) {
            for (size_t i = 0; i < psize; i++) {
                threads.create_thread(boost::bind(&ba::io_service::run, &ioService));
            }
        }

        template <typename F, typename... Args> void add_work(F f, Args... args) {
            ioService.post(boost::bind(f, args...));
        }

        ~Pool() {
            work.reset();
            threads.join_all();
        }

        std::size_t size() const { return threads.size(); }
    };
}

void add1(int &num) { std::cout << ++num << std::endl; }

int main() {
    int temp = 5;

    Experimental::Pool xpool(2);
    std::cout << "Xpool " << xpool.size() << "\n";
    xpool.add_work(add1, std::ref(temp));

    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "End" << std::endl;
}

打印

Xpool 2
6
End
temp: 6

只有约50%的代码。

答案 1 :(得分:0)

你这是致命的问题:

std::shared_ptr<boost::thread> t1(new boost::thread(
    boost::bind(&boost::asio::io_service::run, &ioService)));
threads.add_thread(t1.get());

boost::thread_grouphttp://www.boost.org/doc/libs/1_42_0/doc/html/thread/thread_management.html#thread.thread_management.threadgroup.destructor)的文档中,析构函数的效果是

  

销毁* this并删除组中的所有boost :: thread对象。

因此boost::thread_group将专门参与其线程的资源管理,您无法将这些线程存储在shared_ptr中。

要解决此问题,您需要仔细阅读boost::thread_group的链接文档,使用create_thread()并将其存储为原始指针。