Std::async阻塞/暂停父线程
std::async is blocking/pausing the parent thread
我试图在我的应用程序中使用std::async
和std::future
来创建后台工作守护进程。我似乎得到的行为,表明我没有异步运行在所有!
我已经编译了一小段代码来演示什么不适合我:
#include <cstdio>
#include <future>
#include <chrono>
class AsyncTest {
public:
void run() {
printf("Entering run()n");
std::this_thread::sleep_for(std::chrono::seconds(10));
printf("Exiting run()n");
}
void start() {
printf("Starting daemon...n");
std::async(std::launch::async, &AsyncTest::run, this);
printf("Daemon startedn");
}
};
int main(int argc, char *argv[])
{
AsyncTest* test = new AsyncTest();
test->start();
std::this_thread::sleep_for(std::chrono::seconds(15));
return 0;
}
现在应该很清楚了,所需的功能是这样的:
Starting daemon...
Entering run() <--
Daemon started <-- These two may swap
Exiting run() <------ After 10 seconds
exit() <-- After a further 5 seconds
实际情况如下:
Starting daemon...
Entering run()
Exiting run() <-- After 10 seconds
Daemon started
exit() <-- After 15 seconds
我在BeagleBone Black和Debian Wheezy上运行这个,它只有一个核心-但肯定这应该工作,不管调度操作系统!
std::async
返回一个等待销毁的future
,你必须为它使用一个成员或直接使用std::thread
(分离)
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- Lua/Luajit:暂停当前 Lua 线程
- 暂停和恢复多线程环境中另一个线程的线程C++技术建议
- 无法将线程与第三方库一起暂停
- 暂停和恢复线程的最佳解决方案是什么?
- POSIX:我如何暂停线程
- 超时时暂停线程
- mciSendString 不会暂停从线程播放的声音
- 在不使用自旋锁的情况下暂停空队列上的线程
- 频繁暂停和重启线程的最佳方法是什么?
- 如何暂停线程
- Std::async阻塞/暂停父线程
- 如何在Qt中取消线程暂停
- 在cpp中从父线程暂停和恢复线程