2015-12-15 86 views
1

我在通過R Studio與R Studio Server調用本地庫時遇到問題。這有點令人困惑,因爲我在命令行從R調用它時沒有問題。RStudio在通過Rcpp調用boost :: threadpool時掛起

我寫過一個分析庫,它使用boost的threadpool功能來運行多個線程。我已經剝奪一切下降到最基本,最低的代碼,這將導致該問題 - 這個代碼只是開始的線程池的線程,然後退出他們:

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

回答

3

這可能只是你的成本在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將把它做到你的源代碼包。

相關問題