Request for more functionality for channels

Open
#288 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
api, backend

Research direction

Read the channel implementation and its tests to find where move operations, capacity, and void channel reads and writes are defined. Start by checking which requested behaviors are supported by the current API and how channel operations handle capacity. Done means agreeing on and implementing the desired set of channel features, with tests covering the selected behaviors.

Written by the indexing model from the issue text.

Description

Moving

Channels are currently having their move constructor and operator explicitly deleted. This prevents a lot of sorts of code decomposition since it is not even possible to make a function to abstract some channels-related stuff. Not to mention that it forces library users into writing some really poor code (for example wrapping channel into unique_ptr to enable moving)

struct smth {
  ...
  boost::cobalt::channel<void> chan;
};

// all below is basically impossible 
auto transfer_ownership(smth smth);
boost::cobalt::promise<smth> async_initialization(...);

Occupied capacity requests

Now it is impossible to reason about how much elements is inside the channel. This makes writing a channel-based condition_variable pretty much impossible. There also may be other cases for occupied capacity introspection. This one is just what I have stuck with. Overall, this shall not be a problem to add one extra getter into channel

read/write(N) overload for void channel

Void channel is basically a semaphore. And sometimes it is useful to state that some operations on such semaphore require more parallelism units than others. In such cases something like that has to be written

// for acquiring of N slots
for (size_t i = 0; i < N; ++i) {
  co_await chan.read();
}

// for releasing of N slots
for (size_t i = 0; i < N; ++i) {
  co_await chan.write();
}

There is also a problem that in both of these examples a deadlock is possible if channel has less slots capacity in it than N. And another smaller problem is that if read/write blocks then another one extra slot (up to N) is asked and is possibly blocked once again making otherwise unnecessary context switches in scheduler.

Both of abovementioned problems are avoidable (except for context switches) if user constructs it's own primitive for semaphore using channel. But semaphore is probably needed often enough in different codebases for library to provide it

Batch-processing for data in non-void channels may also be useful in theory. But I cannot imagine a good example for this and thus will not advocate for it

Dominant language
C++
Stars
351
Forks
47
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/cobalt

All issues in boostorg/cobalt

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.