8

我在 Mac OS Xcode 4.3.2 std::async 上使用 C++11 使用相同的线程并且我的代码没有实现并行性。在下面的示例代码中,我想创建 10 个新线程。在每个线程中,我想计算输入变量的平方根并将结果设置为 promise。在主函数中,我想显示从线程计算的结果。我用策略启动::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;

    }
4

1 回答 1

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

此行返回 a futurebut 因为您没有将它分配给任何东西,未来的析构函数在语句末尾运行,它通过调用阻塞并等待结果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";
}
于 2012-05-14T21:45:09.337 回答