如何检查std ::线程是否仍在运行? [英] How to check if a std::thread is still running?
问题描述
如何检查std ::线程是否仍在运行(以平台无关的方式)?
它缺少一个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锁定一个mutex线程和使用互斥体的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.
知道更优雅的方法吗?
更新:要清楚:我想检查线程是否干净地退出。
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
来运行你的任务,那么你可以使用 wait_for 函数
std :: future
来检查线程是否仍以如下方式运行:
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() {
/* 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(std::chrono::seconds(3));
return 8;
});
// Use wait_for() with zero milliseconds to check thread status.
auto status = future.wait_for(std::chrono::milliseconds(0));
// 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
获得未来的对象:
#include <future>
#include <thread>
#include <chrono>
#include <iostream>
int main() {
// 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(std::chrono::seconds(3));
p.set_value(true); // Is done atomically.
});
// Get thread status using wait_for as before.
auto status = future.wait_for(std::chrono::milliseconds(0));
// 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
因为在任务完成之前检查了线程状态。
This is of course because the thread status is checked before the task is finished.
但是再次,它可能比其他人已经提到的更简单:
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() {
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(std::chrono::seconds(3));
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 :: packaged_task
用于 std :: thread
清除溶液比使用 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() {
// Create a packaged_task using some task and get its future.
std::packaged_task<void()> task([] {
std::this_thread::sleep_for(std::chrono::seconds(3));
});
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(std::chrono::milliseconds(0));
// Print status.
if (status == std::future_status::ready) {
// ...
}
t.join(); // Join thread.
}
这篇关于如何检查std ::线程是否仍在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!