0

I want to use boost fibers to accomplish async communication with an external service. I use a set of promise and future for each individual fiber to be called on a separate reader fiber in order to wake up the originator fiber that waits for a future.

The problem is I can not figure out the type of the lambda function. I tried to use std::function and std::bind. But I did not manage to figure out the right type.

The callback is in the following structure:

struct AsyncClientCall {
    std::string response;
    bool status;    // Storage for the status of the RPC upon completion.
    // what is the proper type for this
    std::function<void()> callback;
};

It works for PODs, such as:

    asyncCall->callback = [i = msg] () mutable {
        std::cout << "release the lock: " << i << std::endl; 
    };

But it does not with boost::fibers::promise, since there is no copy constructor:

    asyncCall->callback = [p = std::move(promiseRsp)] () mutable {
        p.set_value();
    };

However there is no problem with the lambda itself, since this works fine

auto  callback = [p = std::move(promiseRsp)] () mutable {
    p.set_value();
};  

So my question is how to get around this properly if I want to use promise for synchronisation between fibers

Mass
  • 123
  • 11
  • Your last example should work fine. According to the [documentation](http://www.boost.org/doc/libs/1_65_1/libs/fiber/doc/html/fiber/synchronization/futures/promise.html) `promise` is movable. Is `promiseRsp` const? – François Andrieux Oct 03 '17 at 19:28
  • @FrançoisAndrieux: yes. But I think it tries to use the copy instead when I want to assign it to a std::function. I just updated the post that it works fine if the type that I assigns it to a auto. – Mass Oct 03 '17 at 19:37
  • 1
    I misunderstood the question. It seems you can't assign a move-only lambda to a `std::function`. See https://stackoverflow.com/questions/25421346/how-to-create-an-stdfunction-from-a-move-capturing-lambda-expression. – François Andrieux Oct 03 '17 at 19:38

0 Answers0