我对编写一个能够运行R脚本的c++程序很感兴趣。出于几个设计原因,我希望创建一个RInside实例、执行一个脚本、获取结果并销毁该实例;所有这些都在一个线程中完成。我知道R不是多线程的,并且不能创建多个RInside实例。但是我可以在隔离的线程中创建单个实例吗?当我尝试这样做时,我的代码会编译,但我在运行时得到以下错误:
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以下是产生错误的代码:
#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;
}我试过按照“编写R扩展”中的建议设置R_CStackLimit = (uintptr_t)-1,但是没有用。
我运行的是ubuntu,R版本2.15.2,RInside版本0.2.10。
有可能做到这一点吗?或者我必须学习像Rserve这样的东西?非常感谢!
发布于 2013-02-12 06:00:17
R是单线程的,将来也很可能保持单线程。RInside会花一些时间来确保它被创建为一个单例;如果你颠覆了它,你会得到上面看到的错误。在同一个可执行文件中,您只获得一个RInside实例,因此每个线程一个实例将不起作用。正如你所经历的。
请参阅我在源代码中包含的示例,了解如何在使用多线程前端时处理单线程后端,例如用于webapps的Qt或Wt库。
从长远来看,我们也许能够做Rserve所做的事情和分支。代码贡献将是受欢迎的,我可能没有时间在这上面工作。
https://stackoverflow.com/questions/14818708
复制相似问题