sdbusplus:通过协程yield_method_call异步调用service的method

sdbusplus还提供了通过协程yield_method_call异步调用的方式:

//async_coroutine.cpp
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace sdbusplus;

void asyncCallCoroutines(shared_ptr conn, boost::asio::yield_context yield)
{
    boost::system::error_code ec;
    cout<<"1 yield_method_call AddInt begin"<yield_method_call(yield, ec, 
        "calculate.service", "/calculate_obj", "calculate_infterface.data", "AddInt", 1, 2);
    if (ec)
    {
        cout<<"yield_method_call failed"<yield_method_call(yield, ec, 
        "calculate.service", "/calculate_obj", "calculate_infterface.data", "AddInt", 3, 4);
    if (ec)
    {
        cout<<"yield_method_call failed"<(io);

    cout<<"asyncCallCoroutines begin"<
编译程序,需要加入协程库:
g++ -o async_coroutine ./async_coroutine.cpp -lsdbusplus -lsystemd -lboost_coroutine
运行程序输出:
asyncCallCoroutines begin
asyncCallCoroutines end
1 yield_method_call AddInt begin
yield_method_call res=3
2 yield_method_call AddInt end
3 yield_method_call AddInt begin
yield_method_call res=7
4 yield_method_call AddInt end

通过输出:
asyncCallCoroutines begin
asyncCallCoroutines end
可以看出协程本身是被异步调用的

但是协程的内部是顺序调用的,有点类似是同步调用的:
1 yield_method_call AddInt begin
yield_method_call res=3
2 yield_method_call AddInt end
3 yield_method_call AddInt begin
yield_method_call res=7
4 yield_method_call AddInt end

你可能感兴趣的:(#,sdbusplus,linux)