Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c+中使用future和promise从函数线程获取返回值+;_C++_Multithreading_Promise_Future - Fatal编程技术网

C++ 如何在c+中使用future和promise从函数线程获取返回值+;

C++ 如何在c+中使用future和promise从函数线程获取返回值+;,c++,multithreading,promise,future,C++,Multithreading,Promise,Future,我有一个下面的代码,我使用future和promise从函数中获取返回值。代码如下: void myfunction2(string msg, std::promise<std::string> && prms) { prms.set_value(msg); } int main() { std::promise<std::string> prms; std::future<std::string> ftr = prm

我有一个下面的代码,我使用
future
promise
从函数中获取返回值。代码如下:

void myfunction2(string msg, std::promise<std::string> && prms)
{
    prms.set_value(msg);
}

int main()
{
    std::promise<std::string> prms;
    std::future<std::string> ftr = prms.get_future();
    while (1)
    {
        thread t1(myfunction2, "Hello thread", std::move(prms));

        cout << "Main loop" << endl;

        std::string str = ftr.get();
        std::cout << str << std::endl;
        t1.join();
        Sleep(2000);
    }
}
因为它在while(1)中,所以当控件返回到
std::string str=ftr.get()时它会引发一些异常

<> >代码>在TestCODE.EXE中0x000、FFDD0F1A88中未处理的异常:微软C++异常:STD::FuturyStor错误在内存位置0x00 000 03FDCFF740.< /代码> /P>
如果我在没有while(1)的情况下运行它,这将起作用,但实际上我需要在`while(1)循环中运行它。如何解决此问题。

承诺的值只能设置一次。要在程序中使用循环,请将promise和future变量降级到循环范围内,如下所示:

int main()
{
    int i = 0;
    while (1)
    {
        std::promise<std::string> prms;
        std::future<std::string> ftr = prms.get_future();

        thread t1(myfunction2, "Hello thread", std::move(prms));

        cout << "Main loop" << endl;

        std::string str = ftr.get();
        std::cout << str << std::endl;
        t1.join();
        Sleep(2000);

        if (++i > 3) break; // no infinite loop
    }
}
intmain()
{
int i=0;
而(1)
{
std::promise-prms;
std::future ftr=prms.get_future();
线程t1(myfunction2,“你好线程”,std::move(prms));
cout根据:

如果没有共享状态或共享状态,则引发异常 已经存储了一个值或异常

在代码中,您似乎试图在承诺中存储不止一次的值。 其次,正如Quimby所指出的,在第二次迭代中,传递的承诺已经从中移除,因此没有共享状态(请参阅关于移动承诺的文档,以了解
std::promise
)。对我来说,不清楚您想要实现什么。无论如何,请记住
std::promise
std::future
基本上是一个一次性的通信通道,共享状态只能设置一次

我重写了您的代码以允许它运行,为每个迭代创建一个新的
promise/future

#include <future>
#include <iostream>
#include <thread>
#include <chrono>
#include <string>



void myfunction2(std::string msg, std::promise<std::string> && prms)
{
    prms.set_value(msg);
}

int main()
{
    while (1)
    {
        std::promise<std::string> prms;
        std::future<std::string> ftr = prms.get_future();

        thread t1(myfunction2, "Hello thread", std::move(prms));

        cout << "Main loop" << endl;

        std::string str = ftr.get();
        std::cout << str << std::endl;
        t1.join();
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}
#包括
#包括
#包括
#包括
#包括
void myfunction2(std::string msg、std::promise和&prms)
{
prms.设定值(msg);
}
int main()
{
而(1)
{
std::promise-prms;
std::future ftr=prms.get_future();
线程t1(myfunction2,“你好线程”,std::move(prms));

循环的第二次迭代是否将使用moved from
prms
,这不是一件好事。此循环的目的是什么?是否确实要在每次迭代中创建另一个循环并等待它?您只能设置一次承诺。删除循环或将承诺置于循环范围内。第一次通过循环时,将创建一个线程使用从
prms
移动的参数创建。第二次通过循环时,同样的操作会再次执行,但
prms
已从中移动,因此不再有用。不要重复使用
promise
future
。每次需要时都在循环内创建一个。谢谢,但我所做的是这就是我的目的。?@SAndrew:你的循环是无限循环。@SAndrew:警告:你的线程没有并发执行。我如何才能并发运行它们?@L.F.谢谢你指出这个问题。我更正了代码
int main()
{
    std::vector<std::pair<std::thread, std::future<std::string>>> threads;

    for (int i = 0; i < 5; i++)
    {
        std::promise<std::string> prms;
        std::future<std::string> fut = prms.get_future();

        thread th(myfunction2, "Hello thread", std::move(prms));
        threads.push_back(make_pair(move(th), move(fut)));
    }

    for (auto& e : threads)
    {
        auto th = move(e.first);
        auto fut = move(e.second);
        std::string str = fut.get();
        std::cout << str << std::endl;
        th.join();
    }    
}
#include <future>
#include <iostream>
#include <thread>
#include <chrono>
#include <string>



void myfunction2(std::string msg, std::promise<std::string> && prms)
{
    prms.set_value(msg);
}

int main()
{
    while (1)
    {
        std::promise<std::string> prms;
        std::future<std::string> ftr = prms.get_future();

        thread t1(myfunction2, "Hello thread", std::move(prms));

        cout << "Main loop" << endl;

        std::string str = ftr.get();
        std::cout << str << std::endl;
        t1.join();
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}