cpp-channel

by ahorn

Experimental Go-style concurrency for C++11

131 Stars 15 Forks Last release: Not found Other 17 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

cpp-channel

Go-style concurrency for C++11

Getting started

To use the library,

#include 
. Here's an example:
void thread_a(cpp::channel c) {
  c.send('A');
  char r = c.recv();

assert('B' == r); }

void thread_b(cpp::channel c) { char r = c.recv(); c.send('B');

assert('A' == r); }

int main() { cpp::channel c;

std::thread a(thread_a, c); std::thread b(thread_b, c);

a.join(); b.join();

return EXIT_SUCCESS; }

As in Go,

cpp::channel
are first-class values. In particular, channel
c
in the example is passed by value to the newly created threads.

The source code repository contains several more examples including the most standard one of them all, Dijkstra's dining philosophers. In fact, it also shows the use of

std::ref
for a
dining_table
struct as these are typically not passed by value.

Documentation

A

cpp::channel
is like a Go channel created with
make(chan T, N)
. If
N
is zero, the channel is synchronous; otherwise, it is asynchronous, as documented in the Go language specification:
  • http://golang.org/ref/spec#Channel_types
  • http://golang.org/ref/spec#Send_statements
  • http://golang.org/ref/spec#Receive_operator

But to simplify the library usage,

cpp::channel
cannot be nil nor closed.

Similar to Go, there are

cpp::ichannel
and
cpp::ochannel
that can only receive and send elements of type T, respectively.

Noteworthy, channels are first-class values. Consequently, we can have channels of channels.

Installation

You only need a C++11-compliant compiler. There are no other external dependencies.

To build the library on a (mostly) POSIX-compliant operating system, execute the following commands from the

cpp-channel
directory:
$ ./autogen.sh
$ ./configure
$ make
$ make test
$ make install

If

make test
fails, you can still install, but it is likely that some features of this library will not work correctly on your system. Proceed at your own risk.

Note that

make install
may require superuser privileges.

The troubleshooting section below has a few additional tips. For advanced configuration options refer to the Autoconf documentation.

Troubleshooting

If

make test
fails with an error that indicates that
libstdc++.so.6
or a specific version of
GLIBCXX
cannot be found, then check out GCC's FAQ.

Bug Reports

You are warmly invited to submit patches as Github pull request, formatted according to the existing coding style.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.