Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ libraries that implement Go goroutines or Go channels? [closed]

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.

like image 876
Derek Thurn Avatar asked Dec 15 '10 20:12

Derek Thurn


People also ask

Can you do something in Goroutines without channels?

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.

How are channels implemented in Go?

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.

When should we use Goroutines?

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.

What are Goroutines and how do they work?

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.


1 Answers

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.

like image 80
timday Avatar answered Sep 28 '22 11:09

timday