简体   繁体   English

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

[英]std::async uses same thread and my code does not achieve parallelism.

I am using C++11 on Mac OS Xcode 4.3.2 std::async uses same thread and my code does not achieve parallelism. 我在Mac OS Xcode 4.3.2上使用C ++ 11 std :: async使用相同的线程,我的代码没有实现并行性。 In sample code below I want to create 10 new threads. 在下面的示例代码中,我想创建10个新线程。 In each thread I want to calculate square root of input variable and set the result in promise. 在每个线程中,我想计算输入变量的平方根并将结果设置为promise。 in main function I want to display the results calculated from threads. 在main函数中我想显示从线程计算的结果。 I am calling std::async with policy launch::async, So I expect it to create a new thread(10 times). 我用策略启动:: 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;

    }

However if I use std::thread, then I can achieve parallelism. 但是,如果我使用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;

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

This line returns a future but because you do not assign it to anything the future's destructor runs at the end of the statement, which blocks and waits for the result by calling std::future::wait() 这一行返回一个future但因为你没有将它分配给任何未来的析构函数在语句结束时运行,它通过调用std::future::wait()来阻塞和等待结果

Why are you manually calling std::async with a promise, when it returns a future anyway? 当它返回未来时,为什么你手动调用带有promise的std::async The point of async is that you don't need to manually use a promise, that's done internally for you. 异步的要点是您不需要手动使用承诺,这是在内部为您完成的。

Rewrite your foo() to return double then call it with 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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM