As std::thread does not have try_join_for() similar to boost, I would like to implement such behavior (on Windows, Visual Studio, does not need to be portable) as:
void my_try_join(int ms)
{
WaitForSingleObject(t.native_handle(), ms);
}
I am a little bit concerned though, whether it is a safe thing to do so. Could not find any documentation that would explicitly tell such mixing std c++ with native calls is permisible (I would like later to be able to either .join() or detach() the thread.
I've written some unit tests to verify that nothing crashes, the following gtest tests pass:
TEST(Test, A)
{
std::thread t([] { std::this_thread::sleep_for(std::chrono::seconds(5)); });
WaitForSingleObject(t.native_handle(), 1000);
EXPECT_TRUE(t.joinable());
t.join();
}
TEST(Test, B)
{
std::thread t([] { std::this_thread::sleep_for(std::chrono::seconds(1)); });
WaitForSingleObject(t.native_handle(), 3000);
EXPECT_TRUE(t.joinable());
t.join();
}
However, I would still like to have some kind of confirmation this code is legit.
Aucun commentaire:
Enregistrer un commentaire