R-Friendly Threading in C++

Provides a C++11-style thread class and thread pool that can safely be interrupted from R. See Nagler (2021) .

Travis-CI Build Status AppVeyor Build Status CRAN version CRAN downloads

Provides R-friendly threading functionality:

  • thread safe versions of Rcpp's checkUserInterrupt() and Rcout,
  • an interruptible thread class that otherwise behaves like std::thread,
  • classes for the thread pool pattern and parallel for loops for easy and flexible parallelism.

The library is header-only, platform-independent, and only requires a C++11-compatible compiler.


For a detailed description of its functionality and examples, see the vignette or the API documentation.


Release version from CRAN:


Latest development version from github:

# install.packages("devtools")

How to use it

with cppFunction

Pass "RcppThread" to the depends argument and "cpp11" to the plugins argument. For example:

Rcpp::cppFunction('void func() { /* actual code here */ }', 
                       depends = "RcppThread", plugins = "cpp11")
with sourceCpp


// [[Rcpp::plugins(cpp11)]] 
// [[Rcpp::depends(RcppThread)]] 

before including any headers in your source code.

in another R package
  1. Add the line CXX_STD = CXX11 to the src/Makevars(.win) files of your package.
  2. Add RcppThread to the LinkingTo field of your DESCRIPTION file.

Automatic override of std::cout and std::thread

There are preprocessor options to replace all occurences of std::cout and std::thread with calls to RcppThread::Rcout and RcppThread::Thread (provided that the RcppThread headers are included first). To enable this, use

#define RCPPTHREAD_OVERRIDE_COUT 1    // std::cout override
#define RCPPTHREAD_OVERRIDE_THREAD 1  // std::thread override

before including the RcppThread headers.


RcppThread 0.5.3

  • Improved handling of exceptions thrown from threads.

RcppThread 0.5.2

  • Limit number of threads in unit tests.

  • Fixed typos in package vignette.

RcppThread 0.5.1


  • Fix portability issues related to native_handle_type.

  • Fix signed/unsigned comparison in parallelFor().

  • Fix signed/unsigned warnings in unit tests.

RcppThread 0.5.0


  • Rcpp is no longer a hard dependency, but only used for unit tests. This avoids unneccessary compilation time during package installation.


  • New vignette available, see browseVignettes("RcppThread").

  • New functions parallelFor() and ForEach() allowing parallel for loops with load balancing. Can also be called as method of a ThreadPool.

  • Options to override std::thread and std::cout with RcppThread equivalents using preprocessor variables RCPPTHREAD_OVERRIDE_THREAD and RCPPTHREAD_OVERRIDE_COUT.

  • Several minor performance optimizations.

RcppThread 0.4.0


  • New function ThreadPool::map() that allows to map a function a list of items.

RcppThread 0.3.0


  • A ThreadPool can now be intantiated with zero threads in the pool. It will then do all work pushed to it in the main thread.

RcppThread 0.2.0


  • ThreadPool has a new method wait() that waits for all jobs to be done without joining the threads. This way the thread pool can be re-used for different types of tasks that need to be run sequentially.

RcppThread 0.1.3


  • Don't check print ouptut of multi-threaded code b/c of random results.

RcppThread 0.1.2


  • Now available for R (>= 3.3.0).


  • Fixed a randomly failing unit test.

RcppThread 0.1.1


  • Default initialize static Rcout instance in header file (#9; couldn't link shared library on r-hub, see discussion in #8)

RcppThread 0.1.0

  • Intial release.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


2.0.0 by Thomas Nagler, a day ago


Report a bug at https://github.com/tnagler/RcppThread/issues

Browse source code at https://github.com/cran/RcppThread

Authors: Thomas Nagler [aut, cre]

Documentation:   PDF Manual  

MIT + file LICENSE license

Suggests testthat, R.rsp, Rcpp

System requirements: C++11

Imported by CDSeq.

Linked to by RcppAlgos, RcppBigIntAlgos, Rforestry, bootUR, dodgr, dtwclust, gesso, matrixprofiler, mvrsquared, rEDM, rayrender, rayvertex, rethnicity, rvinecopulib, samc, tidylda, treenomial, vinereg.

See at CRAN