Skip to content

add ffi for rust's std thread api and use it from c++#48338

Open
jkarneges wants to merge 4 commits intomainfrom
jkarneges/thread-ffi
Open

add ffi for rust's std thread api and use it from c++#48338
jkarneges wants to merge 4 commits intomainfrom
jkarneges/thread-ffi

Conversation

@jkarneges
Copy link
Copy Markdown
Member

@jkarneges jkarneges commented May 1, 2026

After we made logging from C++ go through the Rust logger, I noticed cargo's test harness wasn't capturing the output of logging calls made from subthreads of C++ code. It seems to be a side effect of the test harness and maybe the log crate expecting some kind of Rust management of the thread for capturing to work. This means if C++ code spawns threads using the standard library and makes logging calls from those threads, the Rust logger ends up writing directly to stdout and cluttering the output of cargo test.

As a workaround, this PR adds an FFI and C++ wrapper around Rust's std::thread and updates the C++ code to spawn threads using that instead of through the standard library. We aren't doing anything on the C++ side that would expect some kind of C++ management of threads, so I believe this should be fine. Thread locals are managed by pthreads and shouldn't care which language did the spawning.

On the C++ side, I named the API RustThread instead of simply Thread or thread to avoid conflicts with existing types. It's a little ugly but it also makes it very clear what's happening at the call sites, which can't hurt.

@jkarneges jkarneges requested a review from a team May 1, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant