WaitForMultipleObjects alternative with std::thread?
WaitForMultipleObjects alternative with std::thread?
我的问题是我有多个线程,它们存储在某些容器中,我需要知道何时在某个位置完成的线程。在Windows上,我会做类似的事情:
HANDLE handles[3];
//handles were initialized
DWORD finishingThread = WaitForMultipleObjects(3, handles, false, INFINITE)
有什么方法可以通过std :: thread?
据我所知,标准库中没有什么可以支持这种性质的等待。标准库建立在基础操作系统线程支持的顶部。必要时,标准库只能提供最低的公共分母功能。这意味着它无法包装Win32线程库提供的一些更丰富的功能。
只需使用 std::condition_variable
,然后在完成之前让您的线程fire notify_all()
或 notify_one()
。
然后做cv.wait()
您想要WaitForMultipleObjects()
调用的地方。
这是一个示例,说明如何使用std::thread
和std::future
实现相同的效果,如果您愿意让主线程在轮询线程准备时睡觉(或者您可以让您允许专用线程处理等待)。
考虑此功能,将一系列迭代器的范围纳入std::future
的容器,该容器将阻止至少一个任务完成:
const int TIME_BETWEEN_POLLS_MS = 50;
// Wait (sleep) between polls until a task is finished then return iterator to future.
template <typename Iterator>
Iterator waitForFirst(Iterator first, Iterator last) {
auto it = first;
auto status = std::future_status::timeout;
while (status != std::future_status::ready) {
if (++it == last) { // Rotate in range.
it = first;
}
status = it->wait_for(std::chrono::milliseconds(TIME_BETWEEN_POLLS_MS));
}
return it;
}
现在,如果您有一个与在单独线程上运行的任务的返回值相关联的期货容器(std::future
),则可以简单地使用函数waitForFirst
来使迭代器成为未来获得结果的未来。
// Lets say you have a vector of futures, e.g.
std::vector<std::future<std::thread::id>> futures;
/* Push futures to vector... */
// Block until first task is finished.
// 'it' is iterator to future associated with result.
auto it = waitForFirst(std::begin(futures), std::end(futures));
参见实时示例
尝试ASYNC 组成,这是N3428 C 标准建议的参考实现。
本文在此之前很重要:
损坏的诺言–C 0x期货
以一种不可支配的方式,您还可以调用std :: thread :: bative_handle()与返回一起使用 WaitForMultipleObjects
。
有点迟到,但这是一个示例(适用于Windows):
#include <Windows.h>
#include <thread>
std::shared_ptr<bool> val1 = std::make_shared<bool>(0);
std::shared_ptr<bool> val2 = std::make_shared<bool>(0);
void thread(std::shared_ptr<bool> val) {
*val = true;
OutputDebugString(L"nnthreadnn");
return;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE useless, PWSTR cmdLine, int nCmdShow) {
*val1 = false;
*val2 = false;
std::thread tr1(thread, val1);
std::thread tr2(thread, val2);
tr1.detach();
tr2.detach();
while (!(*val1) || !(*val2)) {
OutputDebugString(L"WAITn");
};
OutputDebugString(L"nnFinishednn");
return 0;
}
您想要的是相当于busost :: thread_group的等效内容。
不存在,但是您可以编写该功能:std :: vector and std :: for_each呼叫join()/waitforsingleobject在每个元素上,或者当然要搜索像cppthreadpool一样执行的第三方
- 在std::thread中,joinable()然后join()线程安全吗
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 分离一个静态常量 std::thread?
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 如何从 std::thread 返回值
- 将 std::thread by 值推送到列表中
- 转发变量参数列表以模拟 std::thread
- 嵌入式设备 -> std::thread -> FreeRTOS?
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何防止 std::thread 在 QT 中冻结 GUI?
- 对带有唯一指针的 std::thread 使用类成员函数时出现编译错误
- 为什么参数在构造 std::thread 时移动两次
- std::thread::_Invoker 使用线程编程时出错
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接
- 为什么编译器抱怨 std::thread 参数在转换为右值后必须是可调用的?
- 将模板(没有规范)传递给 std::thread() 会出现错误:<未解析的重载函数类型>匹配错误
- 使用 std::thread & std::bind 在成员函数中启动线程
- 从gcc 5.4升级至gcc 6.3 std::thread std:ref issue