【问题标题】:RStudio hangs when calling boost::threadpool through Rcpp通过 Rcpp 调用 boost::threadpool 时 RStudio 挂起
【发布时间】:2015-12-15 19:28:39
【问题描述】:

我在使用 R Studio Server 通过 Rcpp 调用本地库时遇到问题。这有点令人困惑,因为我在命令行从 R 调用它时没有任何问题。

我编写了一个分析库,它使用 boost 的线程池功能来运行多个线程。我已经将所有内容都简化为最基本的内容,即会导致问题的最少代码——这段代码只是启动线程池中的线程,然后退出它们:

#include <Rcpp.h>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>

RcppExport SEXP test_thread()
{
BEGIN_RCPP
    double retdbl = 10.4;

    boost::shared_ptr<boost::asio::io_service::work> threadpool_work;
    boost::asio::io_service threadpool_io_service;
    boost::thread_group threadpool_threads;

    threadpool_work.reset(  new         
        boost::asio::io_service::work(threadpool_io_service)  );
    for (int i = 0; i < 6; ++i) {
        threadpool_threads.create_thread(
            boost::bind(&boost::asio::io_service::run, &threadpool_io_service));
    }

    threadpool_io_service.stop();
    threadpool_work.reset();
    threadpool_threads.join_all();

    return(  Rcpp::wrap(retdbl)  );
END_RCPP
}

当我从命令行 R 运行时,没有问题。我得到了双倍的回报。但是,当我通过 R Studio Server 运行时,它要么无休止地挂起,要么在到达 create_thread 语句时崩溃。

我的版本信息是:

  • 回复:R version 3.1.1 (2014-07-10) -- "Sock it to Me"
  • R 工作室:0.99.489
  • Linux:Linux Debian-Jessie 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU/Linux
  • 升压:1.55

【问题讨论】:

    标签: c++ r boost rstudio


    【解决方案1】:

    这可能只是您在 RStudio 中运行的成本线程代码。 RStudio 本身正在为此使用 Boost,并且还与 R 对话——因此似乎两个事件队列混在一起了。我认为除了与他们交谈之外,您无能为力。

    我真的很喜欢 littler 在命令行上以脚本的形式运行更大的作业。自 2006 年以来,它也一直是 Debian 的一部分,您只需一个 apt-get 即可。

    编辑:除了 Rcpp,您可以更紧凑地编写函数

    // [[Rcpp::export]]
    double test_thread() {
        double retdbl = 10.4;
    
        boost::shared_ptr<boost::asio::io_service::work> threadpool_work;
        boost::asio::io_service threadpool_io_service;
        boost::thread_group threadpool_threads;
    
        threadpool_work.reset(  new         
            boost::asio::io_service::work(threadpool_io_service)  );
        for (int i = 0; i < 6; ++i) {
            threadpool_threads.create_thread(
                boost::bind(&boost::asio::io_service::run, &threadpool_io_service));
        }
    
        threadpool_io_service.stop();
        threadpool_work.reset();
        threadpool_threads.join_all();
    
        return(retdbl);
    }
    

    详情请参阅小插图Rcpp Attributes;你可能想在包目录中调用compileAttributes(); RStudio 将对您的源代码包执行此操作。

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 2015-10-21
      • 2012-12-25
      相关资源
      最近更新 更多