C++,async异步

 

 

int Widget::asyncTask(int num)
{
    qDebug() << "async thread id:" << QThread::currentThreadId();
    this_thread::sleep_for(chrono::seconds(3));
    return num * 10;
}

void Widget::fn_test_async()
{
    /*
     * #include <future>
     * #include <thread>
     * using namespace std;
     */
    qDebug() << "main thread id:" << QThread::currentThreadId();
    future<int> future = async(std::launch::async, &Widget::asyncTask, this, 10);
    qDebug() << "run other function";
    int ret = future.get();
    qDebug() << "ret:" << ret;
}

 

posted @ 2025-06-19 20:26  十一的杂文录  阅读(8)  评论(0)    收藏  举报