简体   繁体   English

当通过Rcpp调用boost :: threadpool时,RStudio挂起

[英]RStudio hangs when calling boost::threadpool through Rcpp

I'm having a problem calling a local library through Rcpp with R Studio Server. 我在使用R Studio服务器通过Rcpp调用本地库时遇到问题。 It's a bit perplexing, since I have no issues when I call it from R at the command line. 这有点令人困惑,因为当我在命令行从R调用它时没有问题。

I've written an analytics library which uses boost's threadpool functionality for running multiple threads. 我编写了一个分析库,它使用boost的线程池功能来运行多个线程。 I've stripped everything down to the bare essentials, the minimum code which will cause the problem -- this code just starts the threads in the threadpool, then exits them: 我已经删除了所有内容,这是导致问题的最小代码 - 这段代码只是启动线程池中的线程,然后退出它们:

#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
}

When I run from command-line R, there's no problem. 当我从命令行R运行时,没有问题。 I get the double returned. 我得到双重归来。 However, when I run through R Studio Server, it either hangs endlessly, or it crashes when it gets to the create_thread statement. 但是,当我运行R Studio Server时,它会无休止地挂起,或者在到达create_thread语句时崩溃。

My version info is: 我的版本信息是:

  • R: R version 3.1.1 (2014-07-10) -- "Sock it to Me" R: R version 3.1.1 (2014-07-10) -- "Sock it to Me"
  • R Studio: 0.99.489 R Studio: 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 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
  • boost: 1.55 提升: 1.55

That may just be your cost of running inside RStudio with threaded code . 这可能只是你使用线程代码在RStudio中运行的成本。 RStudio itself is using Boost for this, and also talking to R -- so it seems that two event queues are getting mixed up. RStudio本身正在使用Boost,并且还与R交谈 - 所以看起来两个事件队列正在混淆。 I think short of talking to them there is little you can do. 我认为没有和他们交谈,你几乎无能为力。

I really do like littler for running bigger jobs as scripts on the command-line. 我真的很喜欢利特勒运行更大的工作,如在命令行脚本。 It has been part of Debian since 2006 too as is just an apt-get away for you. 它自2006年以来一直是Debian的一部分,因为它只是一个apt-get你的地方。

Edit: As an Rcpp aside you can write your function more compactly as 编辑:作为一个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);
}

See the vignette Rcpp Attributes for details; 有关详细信息,请参阅插图Rcpp属性 ; you probably want to call compileAttributes() in the package directory; 你可能想在包目录中调用compileAttributes() ; RStudio will do it to your source package. RStudio将对您的源包进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM