Skip to content

set_error() does not get called #156

@Paultok

Description

@Paultok

Hi, the following program has been taken from one of your examples. I modified it to deliberately throw an exception from within await(). Correct me if I am wrong, but in this case the documentation seems to be saying that this would make the library call set_error() at some point in the future, but it does not happen. What am I missing?

// Taken from http://stlab.cc/libraries/concurrency/channel/process/await.html

#include <atomic>
#include <iostream>
#include <thread>
#include <exception>

#include <stlab/concurrency/channel.hpp>
#include <stlab/concurrency/default_executor.hpp>

using namespace std;
using namespace stlab;

struct sum_2
{
    process_state_scheduled _state = await_forever;
    int _counter = 0;
    int _sum = 0;

    void await(int n) {
        throw std::runtime_error{"shouldn't set_error() be called afterwards?"};
        _sum += n;
        ++_counter;
        if (_counter == 2) _state = yield_immediate;
    }

    // http://stlab.cc/libraries/concurrency/channel/process/set_error.html
    void set_error(std::exception_ptr error) { // never called
        cout << "error\n";
    }

    int yield() {
      _state = await_forever;
      auto result = _sum;
      _sum = 0;
      _counter = 0;
      return result;
    }

    auto state() const { return _state; }
};


int main() {
    sender<int> send;
    receiver<int> receive;

    tie(send, receive) = channel<int>(default_executor);

    std::atomic_int r{0};

    auto hold = receive
        | sum_2()
        | [&_r = r](int x) { _r = x; };

    receive.set_ready();

    send(1);
    send(2);

    // Waiting just for illustrational purpose
    while (r == 0) {
        this_thread::sleep_for(std::chrono::milliseconds(1));
    }

    cout << "The sum is " << r.load() << '\n';
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions