簡體   English   中英

如果不調用 `std::future::get()`,下面的代碼片段中是否存在任何潛在問題?

[英]Is there any potential problem in the code snippet below if `std::future::get()` would not be called?

如果不調用std::future::get() ,下面的代碼片段中是否存在任何潛在問題?

我做了幾個測試。 似乎上述代碼在不調用std::future::get()的情況下運行良好,即使std::async需要很長時間才能完成其工作。 真是出乎我的意料。

    #include<future>
    #include<iostream>
    #include<array>
    #include<algorithm>
    #include<thread>
    #include<vector>

    std::array<int, 100000> arr;
    int sum=0;

    struct Wrapper
    {   
        void consume()
        {
            std::cout << "consumer:" << std::this_thread::get_id() << std::endl;
            std::for_each(arr.begin(), arr.end(), [](int val) {sum+=val; });
        }

        void produce()
        {
            std::cout << "producer:" <<std::this_thread::get_id() << std::endl;
            
            int a=0;
            while(true)
            {
                if(a++>1e9)
                {
                    break;
                }
            }
        }
    };


    int main()
    {
        std::fill(arr.begin(), arr.end(), 1);

        std::cout << "main:" <<std::this_thread::get_id() << std::endl;

        Wrapper wrap;

        std::vector<std::future<void>> vec;
        vec.push_back(std::async(std::launch::async, &Wrapper::produce, &wrap));
        vec.push_back(std::async(std::launch::async, &Wrapper::consume, &wrap));

        #ifdef WAIT  //Is there any potencial problem if the block below does not run?
        for(auto& future:vec)
        {
            future.get();
        }
        #endif
    }

根據有關std::future 析構函數的文檔(強調我的):

這些操作不會阻塞共享狀態准備就緒,除非滿足以下所有條件時可能會阻塞:共享狀態是通過調用 std::async 創建的,共享狀態尚未准備好,並且這是對共享狀態的最后引用 (C++14 起)

但它似乎只能由C++14和之后的C++11保證。

在某些情況下,仍然需要顯式調用std::future::get()例如

    #include<future>
    #include<iostream>
    #include<array>
    #include<algorithm>
    #include<thread>
    #include<vector>

    std::array<int, 100000> arr;
    int sum=0;

    struct Wrapper
    {   
        void consume()
        {
            std::cout << "consumer:" << std::this_thread::get_id() << std::endl;
            std::for_each(arr.begin(), arr.end(), [](int val) {sum+=val; });
        }

        void produce()
        {
            std::cout << "producer:" <<std::this_thread::get_id() << std::endl;
            
            int a=0;
            while(true)
            {
                if(a++>1e9)
                {
                    break;
                }
            }
        }
    };


    int main()
    {
        std::fill(arr.begin(), arr.end(), 1);

        std::cout << "main:" <<std::this_thread::get_id() << std::endl;

        Wrapper wrap;

        std::vector<std::future<void>> vec;
        vec.push_back(std::async(std::launch::async, &Wrapper::produce, &wrap));
        vec.push_back(std::async(std::launch::async, &Wrapper::consume, &wrap));

        std::cout << sum << std::endl;
        #if 1
        for(auto& future:vec)
        {
            future.get();
        }
        #endif
        std::cout << sum << std::endl;
    }

以下是上述代碼片段的輸出:

main:140198225385280
0
producer:140198225381120
consumer:140198216988416
100000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM