CJx*_*JxD 2 c++ multithreading asynchronous c++11
我正在尝试使用,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 started\n");
}
};
int main(int argc, char *argv[])
{
AsyncTest* test = new AsyncTest();
test->start();
std::this_thread::sleep_for(std::chrono::seconds(15));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在应该很清楚,所需的功能如下:
Starting daemon...
Entering run() <--
Daemon started <-- These two may swap
Exiting run() <------ After 10 seconds
exit() <-- After a further 5 seconds
Run Code Online (Sandbox Code Playgroud)
实际上是这样的:
Starting daemon...
Entering run()
Exiting run() <-- After 10 seconds
Daemon started
exit() <-- After 15 seconds
Run Code Online (Sandbox Code Playgroud)
我在带有Debian Wheezy的BeagleBone Black上运行它,它只有一个内核-但是可以肯定的是,无论使用调度操作系统,它都可以正常工作!
| 归档时间: |
|
| 查看次数: |
1718 次 |
| 最近记录: |