stlab/libraries

If the last function in a channel returns non void it is never called

Closed this issue · 2 comments

Let's say there is a channel first | ... | last. If last returns something then when sending in the channel last is never called. I think it should be irrelevant.

Below is a test to demonstrate that.

#include <boost/test/unit_test.hpp>
#include <condition_variable>
#include <mutex>
#include <stlab/concurrency/channel.hpp>
#include <stlab/concurrency/default_executor.hpp>

BOOST_AUTO_TEST_CASE(StlabReturnNonVoidInLastTest) {
  stlab::sender<int> sender;
  stlab::receiver<int> receiver;
  std::tie(sender, receiver) = stlab::channel<int>(stlab::default_executor);

  std::mutex mutex;
  std::condition_variable condVar;
  bool isDone = false;

  auto ch = receiver | [&](int x) {
    std::unique_lock<std::mutex> lock(mutex);
    isDone = true;
    condVar.notify_one();
    return x; // With just "return;" it is working as expected.
  };

  receiver.set_ready();
  sender(0);

  std::unique_lock<std::mutex> lock(mutex);
  condVar.wait(lock, [&]() { return isDone; });
}

I am currently traveling. So I will look into it a bit later.

AFAIU, every receivers of the channel need to be set_ready():

	receiver.set_ready();
+     ch.set_ready();
	sender(0);	

With this fix, it works for me.