Closed
Description
I initially assumed that r_sink_mt
would be thread-safe because it uses *_mt
. However, when I tried using it with OpenMP loop, it appears to be non-thread-safe.
Upon reviewing its code, I noticed that it uses Rcpp::Rcout
which is inherently not thread-safe.
rcppspdlog/inst/include/rcpp_sink.h
Lines 38 to 42 in 84fe635
Others using stdout, such as stdout_logger_mt()
, work as expected in the same OpenMp loop. Is there a way with this package to implement thread-safe logger without relying on stdout?
Here is my minimal example.
// [[Rcpp::depends(RcppSpdlog)]]
#include <RcppSpdlog>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_sinks.h>
#include <omp.h>
// [[Rcpp::export]]
void omp_spdlog() {
const int size = 8;
auto console = spdlog::stdout_logger_mt("console");
console->set_pattern("[%n] [thread %t] %v");
console->info("Number of threads: {}", omp_get_max_threads());
#pragma omp parallel for
for (int i = 0; i < size; ++i) {
int thread_id = omp_get_thread_num();
console->info("Thread {} processing index {}", thread_id, i);
}
}
/*** R
omp_spdlog()
*/
If I use spdlog::r_sink_mt("console")
instead, I get error in the R console saying
[console] [thread 438466] Thread 0 processing index 0
Error: C stack usage 17556442392504 is too close to the limit
R 4.4.2 exited unexpectedly: exit code -1
Environment:
> sessionInfo()
R version 4.4.2 (2024-10-31)
Platform: x86_64-apple-darwin20
Running under: macOS Sequoia 15.1.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Asia/Seoul
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.4.2 cli_3.6.3 tools_4.4.2 jsonlite_1.8.8 rlang_1.1.4
Metadata
Metadata
Assignees
Labels
No labels