std :: async使用相同的线程,我的代码没有实现并行性。

时间:2012-05-14 13:51:53

标签: c++ multithreading c++11 future promise

我在Mac OS Xcode 4.3.2上使用C ++ 11 std :: async使用相同的线程,我的代码没有实现并行性。在下面的示例代码中,我想创建10个新线程。在每个线程中,我想计算输入变量的平方根并将结果设置为promise。在main函数中我想显示从线程计算的结果。我用策略启动:: async调用std :: async,所以我希望它创建一个新线程(10次)。

    #include <mutex>
    #include <future>
    #include <thread>
    #include <vector>
    #include <cmath>
    #include <iostream>

    using namespace std;
    mutex iomutex;

    void foo(int i, promise<double> &&prms)
    {
        this_thread::sleep_for(chrono::seconds(2));
        prms.set_value(sqrt(i));
        {
            lock_guard<mutex> lg(iomutex);
            cout << endl << "thread index=> " << i << ", id=> "<< this_thread::get_id();
        }
    }

    int main() 
    {   
        {
            lock_guard<mutex> lg(iomutex);
            cout << endl << "main thread id=>"<< this_thread::get_id();
        }
        vector<future<double>> futureVec;
        vector<promise<double>> prmsVec;

        for (int i = 0; i < 10; ++i) {
            promise<double> prms;
            future<double> ftr = prms.get_future();
            futureVec.push_back(move(ftr));
            prmsVec.push_back(move(prms));

            async(launch::async, foo, i, move(prmsVec[i]));
        }

        for (auto iter = futureVec.begin(); iter != futureVec.end(); ++iter) {
            cout << endl << iter->get();
        }

        cout << endl << "done";

        return 0;

    }

但是如果我使用std :: thread,那么我可以实现并行性。

    #include <mutex>
    #include <future>
    #include <thread>
    #include <vector>
    #include <cmath>
    #include <iostream>

    using namespace std;
    mutex iomutex;

    void foo(int i, promise<double> &&prms)
    {
        this_thread::sleep_for(chrono::seconds(2));
        prms.set_value(sqrt(i));
        {
            lock_guard<mutex> lg(iomutex);
            cout << endl << "thread index=> " << i << ", id=> "<< this_thread::get_id();
        }
    }

    int main() 
    {   
        {
            lock_guard<mutex> lg(iomutex);
            cout << endl << "main thread id=>"<< this_thread::get_id();
        }
        vector<future<double>> futureVec;
        vector<promise<double>> prmsVec;
        vector<thread> thrdVec;
        for (int i = 0; i < 10; ++i) {
            promise<double> prms;
            future<double> ftr = prms.get_future();
            futureVec.push_back(move(ftr));
            prmsVec.push_back(move(prms));

            thread th(foo, i, move(prmsVec[i]));
            thrdVec.push_back(move(th));
        }

        for (auto iter = futureVec.begin(); iter != futureVec.end(); ++iter) {
            cout << endl << iter->get();
        }
        for (int i = 0; i < 10; ++i) {
            thrdVec[i].join();
        }
        cout << endl << "done";

        return 0;

    }

1 个答案:

答案 0 :(得分:17)

            async(launch::async, foo, i, move(prmsVec[i]));

这一行返回future,但因为你没有将它分配给任何东西,未来的析构函数会在语句结束时运行,它会通过调用std::future::wait()

来阻止并等待结果

为什么你在承诺时手动调用std::async,当它返回未来时?异步的要点是您不需要手动使用承诺,这是在内部为您完成的。

重写foo()以返回double,然后使用async

进行调用
#include <mutex>
#include <future>
#include <thread>
#include <vector>
#include <cmath>
#include <iostream>

using namespace std;
mutex iomutex;

double foo(int i)
{
    this_thread::sleep_for(chrono::seconds(2));
    lock_guard<mutex> lg(iomutex);
    cout << "\nthread index=> " << i << ", id=> "<< this_thread::get_id();
    return sqrt(i);
}

int main()
{
    cout << "\nmain thread id=>" << this_thread::get_id();
    vector<future<double>> futureVec;

    for (int i = 0; i < 10; ++i)
        futureVec.push_back(async(launch::async, foo, i));

    for (auto& fut : futureVec)
    {
        auto x = fut.get();
        lock_guard<mutex> lg(iomutex);
        cout << endl << x;
    }

    cout << "\ndone\n";
}