I'm working with a medium-sized C++ code base which is currently entirely single-threaded. The time has come, however, to pursue concurrency and parallelism for performance gains. I'm very interested in the concurrency model of Google's Go programming language, with very lightweight goroutines and a system of communicating channels.
Sadly, for a variety of perfectly valid reasons, the project needs to stay in C++. So my question is: Is there a C++ library that approximates the Go paradigm for parallelism? Specifically, is there an approximation of goroutines or go channels available for C++? My fallback plan is just to use boost::thread.
The application in question is a long-running proprietary simulation for the financial forecasting domain. It's usually CPU bound, but also gets blocked on IO when new data become available. Many of the computations involved are not dependent on previous results and could be fairly easily made to run in parallel. Being able to run the application in a distributed context is a long-term goal, but not one that needs to be solved immediately.
Channels not only work for interactions between a goroutine and the main programs, they also provide a way to communicate between different goroutine. For example, let's create a function that subtracts 3 to every result returned by timesThree but only if it's an even number.
In Go, when a goroutine tries to send data to a full channel, it is added to a queue in the channel struct. This causes the goroutine to block. As space becomes available on the buffer, the Go runtime will pop the first goroutine off the queue and add the data to the buffer. Thus, unblocking the goroutine.
Goroutines are useful when you want to do multiple things simultaneously. For example, if you have ten things you want to do at the same time, you can do each one on a separate goroutine, and wait for all of them to finish.
Goroutines are lightweight, costing little more than the allocation of stack space. The stacks start small and grow by allocating and freeing heap storage as required. Internally goroutines act like coroutines that are multiplexed among multiple operating system threads.
If your aim is primarily speeding up compute things, Intel's TBB (Threading Building Blocks) is (IMHO) a better option than rolling your own inferior version from boost::thread
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With