0

I am interested in writing a c++ program that is capable of running R scripts. For several design reasons I would like to create an instance of RInside, execute a script, grab the result, and destroy the instance; all within a thread. I know that R is not multi-threaded, and that one cannot create multiple instances of RInside. But can I create single instances within isolated threads? When I attempt to do this my code compiles, but I get the following error at run-time:

Error: C stack usage is too close to the limit
Error: C stack usage is too close to the limit
terminate called after throwing an instance of 'Rcpp::binding_not_found'
  what():  binding not found: '.AutoloadEnv'
Aborted

Here is the code that produced the error:

#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <RInside.h>

void *thread_main(void *args){

   RInside R(0,NULL);

   /* hope to execute an R script here */

   printf("--printing from thread--\n");

   return NULL;
}

int main(int argc, char *argv[]){

   pthread_t tid; 
   if( pthread_create(&tid, NULL, thread_main, NULL) ){
      printf("failed to create thread\n");
      return -1;
   } 

   sleep(1); 

   return 0;
}

I have tried setting R_CStackLimit = (uintptr_t)-1 as recommended in Writing R Extension, but to no avail.

I am running ubuntu, R version 2.15.2, RInside version 0.2.10.

Is it possible to accomplish this? or do I have to learn something like Rserve? Thank you so much!

13Kash
  • 1

1 Answers1

0

R is, and will likely remain, single-threaded. RInside goes to some length to ensure it is created as a singleton; if you subvert that you get the errors you see above. Within the same executable, you only get one RInside instance so one-per-thread will not work. As you experienced.

See the examples I include in the source on how to cope with the single-threaded backend when using multi-threaded frontends such as Qt or the Wt library for webapps.

Longer term we may be able to do what Rserve does and fork. Code contributions would be welcome, I probably won't have time to work on this.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725