Go-style concurrency for C++11

747 views
Skip to first unread message

alex...@gmail.com

unread,
Jan 28, 2014, 11:15:55 AM1/28/14
to golan...@googlegroups.com
For fun I've created a small library that brings basic Go-style concurrency to C++11. The design primarily features C++11's <condition_variable>. In particular, there are no other external dependencies. The source code (< 300 LOC) can be found on Github:

  https://github.com/ahorn/cpp-channel

A cpp::channel<T, N> is like a Go channel created with make(chan T, N) where T is an element type and 0 <= N. As in Go, cpp::channel<T, N> objects are first-class values. So channels of channels are possible, as we are used to in Go. Whether this is the best design in the context of C++ is to be determined (due to the concept of copy constructors) but I hope someone finds this library useful in scenarios when Go cannot be incorporated into a project.

Any comments or bug reports are always appreciated. And contributions in form of "git pull" requests are certainly welcome.

With kind regards,
Alex

Matthew Kane

unread,
Jan 28, 2014, 12:23:37 PM1/28/14
to alex...@gmail.com, golang-nuts
Is there a corollary to select statements?
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
matt kane
twitter: the_real_mkb / nynexrepublic
http://hydrogenproject.com

Russel Winder

unread,
Jan 28, 2014, 12:25:45 PM1/28/14
to alex...@gmail.com, golan...@googlegroups.com
On Tue, 2014-01-28 at 08:15 -0800, alex...@gmail.com wrote:
[…]
> https://github.com/ahorn/cpp-channel
>
[…]

Hummm… autotools. I thought SCons, Waf, Premake, CMake had got us beyond
that :-(

But this is header only isn't it, so no build system required?

--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel...@ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder

Alex Horn

unread,
Jan 28, 2014, 1:18:32 PM1/28/14
to Matthew Kane, Russel Winder, golang-nuts
> But this is header only isn't it, so no build system required?

That's right. Autotools is mainly setup for convenience to run the
unit tests (as far as this is possible with a concurrent library). But
I'm open to also have a CMake build (for which gtest works as well).

> Is there a corollary to select statements?

Not yet. Ideally, the basic library features (or a variant therefore)
are shown to be useful first.

With kind regards,
Alex

Ian Lance Taylor

unread,
Jan 28, 2014, 1:47:10 PM1/28/14
to Alex Horn, Matthew Kane, Russel Winder, golang-nuts
On Tue, Jan 28, 2014 at 10:18 AM, Alex Horn <alex...@gmail.com> wrote:
>
>> Is there a corollary to select statements?
>
> Not yet. Ideally, the basic library features (or a variant therefore)
> are shown to be useful first.

I have not looked at your code. I just want to say that the select
statement is the core of Go's channels. Without select, channels are
just a simple communication mechanism. If you want to design a
different implementation of Go's channels, I recommend designing
select first.

Ian

Russel Winder

unread,
Jan 28, 2014, 2:21:26 PM1/28/14
to Ian Lance Taylor, Alex Horn, Matthew Kane, golang-nuts
It is perhaps also worth noting that select is central to CSP and is
implemented in C++CSP2. http://www.cs.kent.ac.uk/projects/ofa/c++csp/

On the other hand Go and goroutines "Just Work"™ :-)

Lars Seipel

unread,
Jan 29, 2014, 4:30:41 PM1/29/14
to Russel Winder, alex...@gmail.com, golan...@googlegroups.com
On Tue, Jan 28, 2014 at 05:25:45PM +0000, Russel Winder wrote:
> Hummm… autotools. I thought SCons, Waf, Premake, CMake had got us beyond
> that :-(

They all suck, just differently so. Autotools at least is
well-understood suckage and has pretty good integration tools for all
kinds of stuff. Heck, even its crappy (relative to Go tools) support for
cross compilation is stellar compared to most of the things you listed.

Alex Horn

unread,
Feb 3, 2014, 6:54:39 PM2/3/14
to Matthew Kane, Ian Lance Taylor, Russel Winder, golang-nuts
> Is there a corollary to select statements?

I've added a restricted select feature to the cpp-channel library.
It's based on concurrency primitives (i.e. condition variables) that
can also be found in languages such as Java. But I am not aware of
another implementation along those lines.

Overall, cpp::internal::_channel<T, N> is succinct but nontrivial
code. So it's not really a corollary but more likely a candidate for
its own proof. Coincidentally, there are lots of comments and
assertions for those who, like me, are interested in formal
verification. In the absence of such a proof, bug reports and/or fixes
are welcome. But note that the case where both ends of a channel are
inside a select statement is currently unsupported. I haven't had time
yet to think about how the design has to change to add this feature
but I suspect this fairly recent paper by Gavin Lowe [1] can give
further insights.

As Ian's earlier comment suggest (and I agree), getting these things
right (and perform well) is difficult. By design, the performance of
cpp-channel is largely determined by the underlying C++11 thread
implementation (e.g. Pthreads) and thread management by the user (e.g.
thread pools). While this means that cpp-channel benefits from regular
upstream improvements, it can be equally a limiting factor due to
generality requirements on C++ standard libraries. But source code is
available for those who are interested in these sort of things.

With kind regards,
Alex

[1] http://www.cs.ox.ac.uk/people/bernard.sufrin/CSO/alt.pdf

Elliott Stoneham

unread,
Feb 4, 2014, 2:35:58 AM2/4/14
to golan...@googlegroups.com, alex...@gmail.com
I doubt it will be as efficient as what you guys are doing, but you could translate some of your existing Go code to C++ using tardisgo.

The project is incomplete and only in its early stages, but it might give you some ideas.

At the moment the project targets C++ via Haxe, it is currently single-threaded and very inefficient.  But I would really love to be able to target C++ directly and efficiently. Clearly channels are a big part of that.

Soon there will be a video on-line of the "Write your own Go compiler" talk that I gave at FOSDEM on Sunday, for now there are only the slides.

Elliott
Reply all
Reply to author
Forward
0 new messages