16 ms·
Coroutines are now in Clang trunk, libc++
- kitd 9y agoSimon Tatham can finally breathe easily.
- iheartmemcache 9y agoSome resources for those who aren't too active in keeping up with C++, these slides are a good quick summary[0]. The talks linked on this page[1] are particularly good, especially the CppCon16 Gor Nishanov talk[2]. Paulo[3] has some interesting things to say (though I think some semantics may have changed since, so grain of salt and all). [0] https://www.slideshare.net/SergeyPlatonov/gor-nishanov-c-coroutines-a-negative-overhead- https://www.slideshare.net/SergeyPlatonov/gor-nishanov-c-cor... Interesting to note, slide #11 uses a tokenizer to demonstrate the usefulness of coroutines. IIRC, Rob Pike used a very similar (maybe his was a lexer/parser?) example in '14 re: Go. [1] http://luncliff.postach.io/post/exploring-msvc-coroutine http://luncliff.postach.io/post/exploring-msvc-coroutine [2] https://channel9.msdn.com/events/CPP/CppCon-2016/CppCon-2016-Gor-Nishanov-C-Coroutines-Under-the-covers https://channel9.msdn.com/events/CPP/CppCon-2016/CppCon-2016... [3]https://paoloseverini.wordpress.com/2015/03/06/stackless-coroutines-with-vs2015/ https://paoloseverini.wordpress.com/2015/03/06/stackless-cor... -- Side-note : Love it or hate it, the C++ community is certainly moving at a vibrant pace. I don't write production C++ code anymore (and haven't for a long time) but I still go out of my way to watch the CppCon talks. IMO, they consistently produce best quality talks just because you have so many excellent programmers from various industries using select subsets of C++ in all sorts of different ways. The people writing games are focused on making sure they're portable 'enough' to hit all the major platforms, without getting too tied to platform-specific low-level calls, while retaining the necessary performance to get satisfactory models/meshes/lighting/collision detection/raytracing/dozens-of-other-things completed within that tiny 16.33 millisecond gap to complete the framebuffer and swap in the next frame. The academics who are doing vast numerical computations will be talking about their new MPI utilizations (and undoubtedly, next years talk will be re: a boatload of RDMA/NUMA optimizations). People complain that C++ is a mishmash of too many concepts (pun not intended). I.e., you can write it in the "C with Objects" style, or the "I use TMP so much my code is basically Haskell", and anywhere in between - but it's that heterogeneity that ends up yielding such high caliber talks.
- Cyph0n 9y ago> People complain that C++ is a mishmash of too many concepts [...] but it's that heterogeneity that ends up yielding such high caliber talks. I agree. Scala gets the same kind of criticism, but just like with C++, you can easily limit Scala to whatever subset suits you (e.g., pure OOP or pure FP). C++ and Scala are among my favorite languages to work with, so I might be slightly biased :P
- pjmlp 9y agoAlso if one looks at any successful programming language in the market that began by fighting complexity, it is easy to observe that they had to eventually embrace complexity to adapt and survive in the market of programming languages. Even if they kept the core simple, the complexity was shovelled into the libraries instead. For example, I doubt there is anyone able to know the complete Python 3.6.1 documentation by heart, plus all major libraries, tools and implementations. Yet it is touted as a simple language.
- w_t_payne 9y agoIt seems to me that the engineering trade-off is found in how we distribute complexity between: (1). The language itself. (2). The tooling (IDE, build system, linters etc...). (3). The standard library. (4). The ecosystem of third-party libraries. (5). The developer's application code. Moving complexity away from (5) might make applications smaller and easier to maintain, but it is done at the risk of increasing the training / learning burden on the development team. If too much complexity ends up in (1), then we create a barrier to entry for new developers and hiring becomes harder. If too much complexity ends up in (4), then we end up with endemic not-invented-here syndrome, because it becomes too hard to learn new libraries. It seems to me that a lot of this is driven by learning and learnability -- any educational specialists around who want to wade in on this discussion?
- pjmlp 9y agoA good example is music. Yes knowing how to play scales and reading notes might seem easy, but mastering an instrument and its variations (e.g. string instruments) takes a whole life.
- colejohnson66 9y agoAt the moment, they are still listed as a WIP[0], but the commit history shows a lot of effort done recently[1] (Ctrl-F "coroutine") [0]: https://clang.llvm.org/cxx_status.html#ts https://clang.llvm.org/cxx_status.html#ts [1]: http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20170522/thread.html http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-2017...
- EricWF 9y agoThe changes literally landed in the past three days; The implementation is theoretically perfect, but once it's (seriously) observed by users it will decay into bugs. That's just basic quantum CS. :-)
- Cieplak 9y agoModern C++ can be very pythonic. Still not as easy as writing Java in IntelliJ but CLion is becoming quite good (if you succumb to CMake). People write shit code in every language; C++ has my favorite abstraction to performance ratio. http://preshing.com/20141202/cpp-has-become-more-pythonic http://preshing.com/20141202/cpp-has-become-more-pythonic
- pjmlp 9y agoIt became my next favourite language in 1993, after I start looking to a Turbo Pascal successor outside MS-DOS/Windows and C failed to impress me. Don't use it as I used to, but the improvements in the last years, specially after the introduction of AST libraries via clang, have done wonders to the eco-system. Now we just need C++ IDEs to catch up with Energize C++ and VA C++ v4, on their Lisp/Smalltalk like experience.
- ShabbosGoy 9y agoThe language has definitely come a long way. Coroutines are definitely a useful addition to clang. It was my #2 choice for writing a middleware for Elasticsearch, however I ultimately chose Golang because I didn't want to deal with CMake.
- pjmlp 9y agoCMake isn't the only build system in town, not being able to use an expressive language due to a specific build system seems a bit too much.
- _ak 9y agoThe struggle with bad tooling is real.
- pjmlp 9y agoI would understand dropping C++ for Swift, Rust, D ..., not Go. Borland C++ for MS-DOS had pre-processor macros for generic data structures, back when ANSI was still trying to figure out how templates should look like. On those days, Java or Oberon derivatives as alternative made sense, because templates weren't even a workable feature in 99% of C++ compilers, so most of us didn't had real exposure to them.
- dis-sys 9y agoCan't wait to see the libc++ coroutines to be declared as production ready. Now I am wondering whether they will be providing some channel implementation similar to the one in Golang in the future. Interesting time for all we C++ fans.
- ingve 9y agoJohn Bandela had a great talk about that at CppCon 2016: "Channels - An alternative to callbacks and futures" [0] [0] https://www.youtube.com/watch?v=N3CkQu39j5I https://www.youtube.com/watch?v=N3CkQu39j5I
- EricWF 9y agoLibc++ coroutines are production ready! But seriously, the library implementation for coroutines is just a shallow wrapper around compiler builtins. Therefore libc++ is probably bug free, but Clang certainly isn't. EDIT: "probably bug free" == famous last words.
- softwarelimits 9y agoWhat would be the best learning path to take for young people wanting to learn C++ today, especially with a focus on free software? Thanks for your attention!
- codebeaker 9y agoExcellent question, and one for which I've not found a satisfactory answer. Perhaps some of the HN wisends can chime in. I believe with some linters (clang, etc) you can encourage your tools to slap your wrists if you code in a bad style using outdated techniques.
- Cieplak 9y agoFirst decide if it's really something worth your time. Then, spend several years playing with the std lib and maybe dabble with Boost. Read stackoverflow threads tagged with [c++] sorting by vote count descending. Read the tests in popular github c++ projects. Read A Tour of C++. Most importantly, write and rewrite lots of code, test it, debug it, and try out cool ideas you think of. If you get stuck, context shift to another task where you're not spinning your wheels.
- pjmlp 9y agoFirst of all, and most important one, learn C++ idioms, not C. This means making use of library types for memory allocation, vectors and strings. There is a place for old C style low level features, but those should come later, after the safer concepts are already ingrained. Kate Gregory's “Stop Teaching C" talk at CppCon 2015 explains better than me. https://www.youtube.com/watch?v=YnWhqhNdYyk https://www.youtube.com/watch?v=YnWhqhNdYyk Get a nice IDE, QtCreator, Clion, VC++ Community, KDevelop, XCode. Besides the "Tour of C++", check Stephan T. Lavavej talks at Channel 9. "Core C++ Lectures" https://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C- https://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavav... "Advanced STL", these ones only when you already have a good understanding of STL as user https://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Advanced-STL https://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavav... Also check video presentations from NDC, ACCU, CppCon, CppNow and C9 Going Native.
- swetabhsuman8 9y agoDon’t Be Fooled By Fake facebook Apps http://hackernucleus.com/phishing-dont-be-fooled-by-fake-facebook-apps/ http://hackernucleus.com/phishing-dont-be-fooled-by-fake-fac...
- pedrow 9y agoDoes anyone know, if coroutines are implemented by the compiler, will they also be available for C? Specifically, I'm thinking about the brilliant Io language which uses its own cross-platform C coroutine library. If this could be done 'natively' I wonder if it would simplify the code?
- leeter 9y agoThey are, however I'm not sure the machinery of a coroutine will translate well to C. Gor does a pretty good breakdown of what's actually happening in this video https://www.youtube.com/watch?v=8C8NnE1Dg4A https://www.youtube.com/watch?v=8C8NnE1Dg4A which I would suggest watching. Not sure it can't be done, but it would require a good design. Personally I think that a libdispatch approach works better for C, particularly with the blocks extension.
- nickbauman 9y agoThis is how I understand the two approaches to concurrency: 1) Use preemptive system threads that can execute in parallel. A task requiring simultaneous waiting is given an operating system thread of its own so it can block without stopping the entire program. But threads require significant memory and other resources per thread. Also, the operating system can arbitrarily interleave the execution of system threads, requiring the programmer to carefully protect shared resources with locks and condition variables, which is exceedingly error-prone. 2) Have a single-threaded program, where that single thread runs an event loop whose job is to react to external events by invoking a callback function that has been registered. While it doesn't require the same kind of complex synchronization that preemptive threads do, the inverted control structure of this approach requires your own control flow to thread awkwardly through the system's event loop, leading to a maze of event callbacks. Coroutines work with the latter style in an attempt to tame its complexity. They are themselves complex, however, and, in my opinion, that complexity doesn't pull its weight when you consider in the end you only have one thread. Can anyone tell me what I'm missing or how these problems with these approaches have been solved in places?
- snissn 9y agoYou can have 2) with the additional complexity / upside of multiple threads
- maxpolun 9y agoYou can have multiple threads with coroutines. However you generally only have one thread per CPU/hardware thread, and use async io within that thread. This is the general architecture of nginx for example (though it doesn't use coroutines, just callback based async io).
- strictfp 9y agoOr 3) you do like golang and erlang and build really lightweight green threads so you get the programming model of approach 1 and nearly the same speed as number 2. This of course requires extra effort to prevent blocking of OS threads and in the case where the OS expects thread locality. But IMHO it's the superior approach. The best solution would be if OSes themselves would provide really lightweight threads. And I suspect that they will, eventually.
- crudbug 9y agoI think with all the async / await noise, the simplicity of co-routines is usually forgotten. IMHO, they are the right abstraction on top of event-loops. Every major server platform, especially - JVM, CLR, should support them. I would be very much interested in context-switch data of server applications for Threads vs. Coroutines loads.
- vvanders 9y agoYup, I never understand the need to bring a heavyweight thread to bear when coroutines are the actual semantics that most people want. It's one of my favorite features of Lua and lets you do async-anything in a really nice and clean manner with minimal overhead.
- tobz 9y agoI'd say that this is roughly the case with .NET, in terms of async/await. IIRC, you're using a thread pool under the hood, and you sort of have to opt-in: if you're trying to write your own async code, for example, you need to schedule it in a way that's slightly more complicated than just saying `go myFunc()`, but it's possible. As for the JVM, libraries like Akka and Quasar already provide this, although possibly not as performantly as if the runtime itself provided it?
- int_19h 9y agoStrictly speaking, whether you're using the thread pool under the hood or not depends on the task scheduler, which is pluggable. The default one that new threads get is indeed going to dispatch to the thread pool (on different threads at that, not the same one). But it can be pretty much anything. Async/await doesn't really care about any of that, it just creates continuations for you, and hands them over to the scheduler.
- nosefouratyou 9y agoI found this to be a good article about the importance of coroutines compared to continuation-passing style: http://orthecreedence.github.io/cl-async/2012/11/07/missing-coroutines-in-common-lisp.html http://orthecreedence.github.io/cl-async/2012/11/07/missing-...
- Findeton 9y agoWhere can I see some examples or documentation on using coroutines in C++?
- throwaway-1209 9y agoI wish Google would open source / standardize their implementation of green threads ("fibers" as they're called internally). You basically write linear programs with those (with proper stack traces etc) and they're cooperatively concurrent in user space, possibly over a fixed size preemptively concurrent kernel thread pool. Very nifty and simplifies things a lot. Never seen anything like it on the outside.
- Johnny_Brahms 9y agoAndy Wingo's fibers for guile scheme is similar: https://github.com/wingo/fibers/wiki/Manual https://github.com/wingo/fibers/wiki/Manual Really neat, and the implementation is really a testament to the power of delimited continuations.
- anon78539737567 9y agoI assume that you are trolling. Anyway, be aware that performance of Go programs are still behind of C++, so it is not a real replacement
- throwaway-1209 9y agoI'm not talking about Go. Google fibers are C++.
- nick_ 9y agoThank you for mentioning this! I've been pondering this kind of fiber+thread design for a few days and it's nice to have some validation of the idea since I didn't know of any similar designs.