Calling multi-threaded C++ code from R has its perils. Since the R interpreter is single-threaded, one must not check for user interruptions or print to the R console from multiple threads. One can, however, synchronize with R from the main thread. The R package RcppThread (current version 1.0.0) contains a header only C++ library for thread safe communication with R that exploits this fact. It includes C++ classes for threads, a thread pool, and parallel loops that routinely synchronize with R. This article explains the package's functionality and gives examples of its usage. The synchronization mechanism may also apply to other threading frameworks. Benchmarks suggest that, although synchronization causes overhead, the parallel abstractions of RcppThread are competitive with other popular libraries in typical scenarios encountered in statistical computing.
翻译:R 从 R 调用的多线 C++ 代码有其风险。 由于 R 解释器是单线读取的, 人们决不能检查用户中断或打印到 R 控制台的多重线条。 但是, 您可以从主线与 R 同步。 R 套件 RcppThread (当前版本 1. 0.0) 包含一个 C++ 库, 用于与 R 进行线条安全通信, 从而利用这一事实。 它包括线条的 C++ 类、 线条库和与 R 经常同步的平行循环。 文章解释了软件包的功能, 并举例说明了它的用法。 同步机制也可以适用于其他线条框架 。 基准显示, 尽管同步导致间接费用, RcppThread 的平行抽象在统计计算中遇到的典型情况下与其他流行的图书馆具有竞争力 。