ThreadLocalStream in C++ (#3405)

This commit is contained in:
Cheng
2026-04-15 15:46:11 -07:00
committed by GitHub
parent fd8e849e26
commit dec6b4d10f
9 changed files with 80 additions and 43 deletions
+22
View File
@@ -104,6 +104,28 @@ TEST_CASE("test new stream in threads") {
}
}
TEST_CASE("test thread local stream") {
auto s = new_thread_local_stream(default_device());
int result = sum(arange(10, s)).item<int>();
std::atomic<int> finished = 0;
std::vector<std::thread> threads;
int num_threads = 4;
for (int i = 0; i < 4; ++i) {
threads.emplace_back([&]() {
int r = sum(arange(10, s)).item<int>();
CHECK_EQ(result, r);
finished += 1;
clear_streams();
});
}
for (auto& t : threads) {
t.join();
}
CHECK_EQ(finished, num_threads);
}
TEST_CASE("test get streams") {
// Initialize default CPU stream before querying
default_stream(Device::cpu);