poco异步等待ActiveResult

本文介绍并演示了如何使用Poco库中的ActiveMethod特性实现异步加法操作。通过定义ActiveAdder类和其成员函数addImpl,我们创建了一个可以接收一对整数参数并返回它们之和的ActiveMethod实例。在main函数中,我们调用了这个ActiveMethod实例,并在执行其他任务的同时等待结果。此示例展示了Poco库在处理异步任务方面的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "Poco/ActiveMethod.h"
#include "Poco/ActiveResult.h"
#include <utility>
#include <iostream>

using Poco::ActiveMethod;
using Poco::ActiveResult;

class ActiveAdder
{
public:
    ActiveAdder(): add(this, &ActiveAdder::addImpl)
    {
    }

    ActiveMethod<int, std::pair<int, int>, ActiveAdder> add;

private:
    int addImpl(const std::pair<int, int>& args)
    {
        return args.first + args.second;
    }
};

int main(int argc, char** argv)
{
    ActiveAdder adder;

    ActiveResult<int> sum = adder.add(std::make_pair(1, 2));
    // do other things
    for(int i=0;i<10;++i)
        std::cout<<i+1<<std::endl;
//    sum.wait();
    while(sum.available())
        break;
    std::cout << sum.data() << std::endl;

    return 0;
}

转载于:https://www.cnblogs.com/L-Arikes/p/4969536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值