2013-02-11 35 views
0

我有兴趣编写一个能够运行R脚本的C++程序。出于几个设计原因,我想创建一个RInside实例,执行脚本,获取结果并销毁实例;全部在一个线程内。我知道R不是多线程的,并且不能创建多个RInside实例。但是我可以在独立线程中创建单个实例吗?当我试图做到这一点我的代码编译,但我会在运行时出现以下错误:在线程内创建一个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_CStackLimit = (uintptr_t)-1为在Writing R Extension中推荐,但无济于事。

我在运行ubuntu,R版本2.15.2,RInside版本0.2.10。

可以做到这一点吗?还是我必须学习Rserve? 非常感谢!

回答

0

R是并将可能保持单线程。 RInside花了一些时间来确保它被创建为一个单身人士;如果你颠覆,你会得到你在上面看到的错误。在同一个可执行文件中,您只能获得一个RInside实例,因此每个线程无法工作。如你所见。

有关如何在使用多线程前端(如Qt或Web应用程序的Wt库)时处理单线程后端请参阅源代码中的示例。

长期来看,我们可能能够做到Rserve所做的工作和分叉。代码贡献将是受欢迎的,我可能没有时间去处理这个问题。

相关问题