开发者社区> 问答> 正文

承诺可以使用结构类型吗?

我想知道是否可以使用结构作为承诺的返回类型。我查看了C++引用,没有提到对允诺类型的限制。我可以找到的所有示例都使用了INT、Long、Double、String类型,但从不使用结构。我修改了Microsoft(TM)网站上的一个示例,我稍微修改了这个示例(代码,而不是站点)。它应该能工作,但是我一直有这样的错误:

k.cpp: In function ‘void DoSomeWork(int, std::promise<toBeReturned>&)’:
k.cpp:25:9: error: ‘class std::promise<toBeReturned>’ has no member named ‘a’
     ret.a.set_value(it);
         ^
compilation terminated due to -Wfatal-errors.

看不出我是犯了一个不可原谅的新手错误,还是遇到了一些我还不明白的事情。

以下这是密码

#include <chrono>
#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <thread>         // std::thread
#include <future>         // std::promise, std::future

using namespace std;
using namespace std::chrono;

struct toBeReturned
    {
    int a;
    int b;
    int c;
    };

void DoSomeWork(int numIters, promise<toBeReturned>& ret)
    {
    int it = 0;
    for ( ; it < numIters; ++it)
        {
        cout << "Thread " << this_thread::get_id() << " working..." << endl;
        this_thread::sleep_for(milliseconds(1000));
        }
    ret.a.set_value(it);
    ret.b.set_value(it*10);
    ret.c.set_value(it*100);
    }

void TestPromise()
    {
    int its = 10;
    int i=0;
    bool notDoneYet=true;
    promise<int> myPromise;
    future<int> myFuture = myPromise.get_future();
    thread aThread(DoSomeWork, its, ref(myPromise));
    std::future_status myFutureStatus;

    while(notDoneYet)
        {
        i++;
        try 
            {
            myFutureStatus=myFuture.wait_for(milliseconds(100));
            if(myFuture.valid() && myFutureStatus != future_status::ready)
                {
                cout << "Thread " << this_thread::get_id() << " waiting..." << endl;
                }
            else if(myFuture.valid() && myFutureStatus == std::future_status::ready)
                {
                toBeReturned valuesToBeReturned;
                try
                    {
                    valuesToBeReturned=myFuture.get();
                    cout << "Work iterations -> a " << a <<  " b " << b <<  " c " << c  << endl;
                    aThread.join();
                    notDoneYet=false;
                    }
                catch (std::future_error& e)
                    {
                    cout << "(2) future_error caught: " << e.code().message() << endl;
                    }
                }
            }
        catch (future_error& e)
            {
            cout << "(1) future_error caught: " << e.code().message() << endl;
            }
        }
    }

int main()
    {
    TestPromise();
    return 0;
    }

展开
收起
aqal5zs3gkqgc 2019-12-04 22:36:15 1045 0
1 条回答
写回答
取消 提交回答
  • 非常感谢你清晰而有用的答案。实际上,这是一个类型错配的混乱。

    以下是修正后的代码:

    #include <chrono>
    #include <iostream>       // std::cout
    #include <functional>     // std::ref
    #include <thread>         // std::thread
    #include <future>         // std::promise, std::future
    
    using namespace std;
    using namespace std::chrono;
    
    struct toBeReturned
        {
        int a;
        int b;
        int c;
        };
    
    void DoSomeWork(int numIters, promise<toBeReturned>& ret)
        {
        int it = 0;
        toBeReturned tmp; // **********************
        for ( ; it < numIters; ++it)
            {
            cout << "Thread " << this_thread::get_id() << " working..." << endl;
            this_thread::sleep_for(milliseconds(1000));
            }
        tmp.a=it; // **********************
        tmp.b=it*10; // **********************
        tmp.c=it*100; // **********************
        ret.set_value(tmp); // **********************
        }
    
    void TestPromise()
        {
        int its = 10;
        int i=0;
        bool notDoneYet=true;
        promise<toBeReturned> myPromise; // **********************
        future<toBeReturned> myFuture = myPromise.get_future(); // **********************
        thread aThread(DoSomeWork, its, ref(myPromise));
        std::future_status myFutureStatus;
    
        while(notDoneYet)
            {
            i++;
            try 
                {
                myFutureStatus=myFuture.wait_for(milliseconds(100));
                if(myFuture.valid() && myFutureStatus != future_status::ready)
                    {
                    cout << "Thread " << this_thread::get_id() << " waiting..." << endl;
                    }
                else if(myFuture.valid() && myFutureStatus == std::future_status::ready)
                    {
                    toBeReturned valuesToBeReturned;
                    try
                        {
                        valuesToBeReturned=myFuture.get();
                        cout << "Work iterations -> a " << valuesToBeReturned.a <<  " b " << valuesToBeReturned.b <<  " c " << valuesToBeReturned.c  << endl; // **********************
                        aThread.join();
                        notDoneYet=false;
                        }
                    catch (std::future_error& e)
                        {
                        cout << "(2) future_error caught: " << e.code().message() << endl;
                        }
                    }
                }
            catch (future_error& e)
                {
                cout << "(1) future_error caught: " << e.code().message() << endl;
                }
            }
        }
    
    int main()
        {
        TestPromise();
        return 0;
        }
    
    2019-12-04 22:37:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
软件定义的数据智能 立即下载
用计算和数据去改变整个世界 立即下载
软件定义存储-面向云的企业级存储重构 立即下载