初步测试了一下C++11的async/future

代码如下:

/////////////////////////////////////////////////
//        
//
//            C++11 async test
//
//

#include "pch.h"
#include <iostream>

#include <future>

struct Task
{
    int operator () (int i)
    {
        std::cout << "task run" << std::endl;
        return i + 10;
    }
};

int main(int argc, char * const argv[])
{
    Task t;
    std::future<int> result = std::async(std::launch::async, t, 16);
    for (int i = 0; i < 100; ++i)
    {
        std::cout << "main thread" << std::endl;
    }
    std::cout << result.get() << std::endl;

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/len3d/p/13202437.html