Experimental Go-style concurrency for C++11
Go-style concurrency for C++11
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::channelare first-class values. In particular, channel
cin 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::reffor a
dining_tablestruct as these are typically not passed by value.
A
cpp::channelis like a Go channel created with
make(chan T, N). If
Nis zero, the channel is synchronous; otherwise, it is asynchronous, as documented in the Go language specification:
But to simplify the library usage,
cpp::channelcannot be nil nor closed.
Similar to Go, there are
cpp::ichanneland
cpp::ochannelthat can only receive and send elements of type T, respectively.
Noteworthy, channels are first-class values. Consequently, we can have channels of channels.
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-channeldirectory:
$ ./autogen.sh $ ./configure $ make $ make test $ make install
If
make testfails, 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 installmay require superuser privileges.
The troubleshooting section below has a few additional tips. For advanced configuration options refer to the Autoconf documentation.
If
make testfails with an error that indicates that
libstdc++.so.6or a specific version of
GLIBCXXcannot be found, then check out GCC's FAQ.
You are warmly invited to submit patches as Github pull request, formatted according to the existing coding style.