[R] problem with Rcpp and boost threadpool

Ken Gosier ken_gosier at yahoo.com
Tue Dec 15 20:44:15 CET 2015

I'm having a problem calling a local library through Rcpp with R Studio Server. It's a bit perplexing, since I have no issues when I call it from R at the command line. 

I've written an analytics library which uses boost's threadpool functionality for running multiple threads. 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() 
    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) { 
            boost::bind(&boost::asio::io_service::run, &threadpool_io_service)); 


    return(  Rcpp::wrap(retdbl)  ); 

When I run from command-line R, there's no problem, 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. 

My version info is: 

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

Many thanks for any help!

More information about the R-help mailing list