問題描述
如何檢查 std::thread
是否仍在運行(以獨立于平臺的方式)?它缺少 timed_join()
方法,而 joinable()
并不適用于此.
How can I check if a std::thread
is still running (in a platform independent way)?
It lacks a timed_join()
method and joinable()
is not meant for that.
我想過在線程中用std::lock_guard
鎖定一個互斥鎖,并使用互斥鎖的try_lock()
方法來判斷它是否仍然被鎖定(線程正在運行),但對我來說似乎不必要地復雜.
I thought of locking a mutex with a std::lock_guard
in the thread and using the try_lock()
method of the mutex to determine if it is still locked (the thread is running), but it seems unnecessarily complex to me.
你知道更優(yōu)雅的方法嗎?
Do you know a more elegant method?
更新: 明確地說:我想檢查線程是否干凈地退出.一個掛起"的線程被視為為此目的而運行.
Update: To be clear: I want to check if the thread cleanly exited or not. A 'hanging' thread is considered running for this purpose.
推薦答案
如果你愿意使用 C++11 std::async
和 std::future
來運行你的任務,然后你可以利用 std::future
的 wait_for
函數(shù)來檢查線程是否仍在以這樣一種簡潔的方式運行:>
If you are willing to make use of C++11 std::async
and std::future
for running your tasks, then you can utilize the wait_for
function of std::future
to check if the thread is still running in a neat way like this:
#include <future>
#include <thread>
#include <chrono>
#include <iostream>
int main() {
using namespace std::chrono_literals;
/* Run some task on new thread. The launch policy std::launch::async
makes sure that the task is run asynchronously on a new thread. */
auto future = std::async(std::launch::async, [] {
std::this_thread::sleep_for(3s);
return 8;
});
// Use wait_for() with zero milliseconds to check thread status.
auto status = future.wait_for(0ms);
// Print status.
if (status == std::future_status::ready) {
std::cout << "Thread finished" << std::endl;
} else {
std::cout << "Thread still running" << std::endl;
}
auto result = future.get(); // Get result.
}
如果你必須使用 std::thread
那么你可以使用 std::promise
來獲取未來的對象:
If you must use std::thread
then you can use std::promise
to get a future object:
#include <future>
#include <thread>
#include <chrono>
#include <iostream>
int main() {
using namespace std::chrono_literals;
// Create a promise and get its future.
std::promise<bool> p;
auto future = p.get_future();
// Run some task on a new thread.
std::thread t([&p] {
std::this_thread::sleep_for(3s);
p.set_value(true); // Is done atomically.
});
// Get thread status using wait_for as before.
auto status = future.wait_for(0ms);
// Print status.
if (status == std::future_status::ready) {
std::cout << "Thread finished" << std::endl;
} else {
std::cout << "Thread still running" << std::endl;
}
t.join(); // Join thread.
}
這兩個例子都會輸出:
Thread still running
這當然是因為在任務完成前檢查了線程狀態(tài).
This is of course because the thread status is checked before the task is finished.
但話說回來,像其他人已經(jīng)提到的那樣做可能更簡單:
But then again, it might be simpler to just do it like others have already mentioned:
#include <thread>
#include <atomic>
#include <chrono>
#include <iostream>
int main() {
using namespace std::chrono_literals;
std::atomic<bool> done(false); // Use an atomic flag.
/* Run some task on a new thread.
Make sure to set the done flag to true when finished. */
std::thread t([&done] {
std::this_thread::sleep_for(3s);
done = true;
});
// Print status.
if (done) {
std::cout << "Thread finished" << std::endl;
} else {
std::cout << "Thread still running" << std::endl;
}
t.join(); // Join thread.
}
還有與 std::thread
一起使用的 std::packaged_task
比使用 std::promise
更簡潔的解決方案:
There's also the std::packaged_task
for use with std::thread
for a cleaner solution than using std::promise
:
#include <future>
#include <thread>
#include <chrono>
#include <iostream>
int main() {
using namespace std::chrono_literals;
// Create a packaged_task using some task and get its future.
std::packaged_task<void()> task([] {
std::this_thread::sleep_for(3s);
});
auto future = task.get_future();
// Run task on new thread.
std::thread t(std::move(task));
// Get thread status using wait_for as before.
auto status = future.wait_for(0ms);
// Print status.
if (status == std::future_status::ready) {
// ...
}
t.join(); // Join thread.
}
這篇關于如何檢查 std::thread 是否仍在運行?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!