首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >std:异步在某些msvc版本中失败,解决方法是什么?

std:异步在某些msvc版本中失败,解决方法是什么?
EN

Stack Overflow用户
提问于 2022-11-02 16:16:26
回答 1查看 67关注 0票数 1

以下代码:

代码语言:javascript
复制
#include <vector>
#include <algorithm>
#include <thread>
#include <future>
#include <iostream>


int main() {
    std::vector<int> v(1000);
    for(unsigned i = 0; i < v.size(); i++) {
        v[i] = i;
    }
    int bb = 2;
    std::cout << v.back() << std::endl;
    auto f = [&](int& x) {x = 2*x; bb=4; };
    
    std::async(std::launch::async, std::for_each<decltype(v.begin()),decltype(f)>,
           v.begin(), v.end(), f);

    std::cout << v.back() << std::endl;
    std::cout << "bb: " << bb;
    return 0;
}

未能使用msvc版本< 19.32进行编译。例如,msvc = 19.29 (在godbolt.org上编译)的错误如下所示:

代码语言:javascript
复制
example.cpp
<source>(16): warning C4834: discarding return value of function with 'nodiscard' attribute
C:/data/msvc/14.31.31108/include\future(314): error C2280: 'main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37> &main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>::operator =(const main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37> &)': attempting to reference a deleted function
<source>(14): note: see declaration of 'main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>::operator ='
<source>(14): note: 'main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37> &main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>::operator =(const main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37> &)': function was explicitly deleted
C:/data/msvc/14.31.31108/include\future(309): note: while compiling class template member function 'void std::_Associated_state<_Ty>::_Set_value_raw(_Ty &&,std::unique_lock<std::mutex> *,bool)'
        with
        [
            _Ty=main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>
        ]
C:/data/msvc/14.31.31108/include\future(305): note: see reference to function template instantiation 'void std::_Associated_state<_Ty>::_Set_value_raw(_Ty &&,std::unique_lock<std::mutex> *,bool)' being compiled
        with
        [
            _Ty=main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>
        ]
C:/data/msvc/14.31.31108/include\future(722): note: see reference to class template instantiation 'std::_Associated_state<_Ty>' being compiled
        with
        [
            _Ty=main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>
        ]
C:/data/msvc/14.31.31108/include\future(720): note: while compiling class template member function 'std::_State_manager<_Ty>::~_State_manager(void) noexcept'
        with
        [
            _Ty=main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>
        ]
C:/data/msvc/14.31.31108/include\future(879): note: see reference to function template instantiation 'std::_State_manager<_Ty>::~_State_manager(void) noexcept' being compiled
        with
        [
            _Ty=main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>
        ]
C:/data/msvc/14.31.31108/include\future(860): note: see reference to class template instantiation 'std::_State_manager<_Ty>' being compiled
        with
        [
            _Ty=main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>
        ]
<source>(17): note: see reference to class template instantiation 'std::future<main::<lambda_fa7e30e7ff267c277ebbd82c8a7f9e37>>' being compiled

https://godbolt.org/z/WsovKcnnc

它是用gcc、icc、clang和msvc (>=19.32) (使用-lpthread)成功编译的。

是否可以使用msvc版本= 19.29 (VS19)进行编译?

我尝试了/std:c++latest选项,但这并没有什么区别。

EN

回答 1

Stack Overflow用户

发布于 2022-11-03 12:02:49

如果Marek认为MSVC的STL实现是正确的,那么您可以通过使用std::packaged_task和std::线程来避免调用std::异步。

代码语言:javascript
复制
#include <thread>
#include <future>

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

int main()
{
    std::vector<int> v(100);
    std::iota(v.begin(), v.end(), 0);
    int bb = -2;
    auto f = [&] (int& x) {
        x = 2 * x;
        bb = 4;
    };
    using Iter = decltype(v.begin());
    using Fun = decltype(f);
    auto foreach = [] (Iter begin, Iter end, Fun f) {
        std::for_each<Iter, Fun>(begin, end, f);
    };
    std::packaged_task<void(Iter, Iter, Fun)> task(foreach);
    auto future = task.get_future();
    std::thread t(std::move(task), v.begin(), v.end(), f);

    future.get();
    t.join();
    //
    std::cout << v.back() std::endl;
    std::cout << "bb: " << bb << std::endl;
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74292454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档