On Friday, 2 January 2015 at 17:39:19 UTC, Matt wrote:
I'm trying to write a small 3D engine, and wanted to place the
physics in a separate thread to the graphics, using events,
possibly std.concurrency, to communicate between them.
How, then, do I pass large amounts of data between threads? I'm
thinking the physics world state (matrix for each object,
object heirarchies, etc) being passed to the graphics thread
for rendering.
I'd assumed that I would use Mutex, or ReadWriteMutex, but I
have no idea how to build code using these classes, if this is
even the right way to go about this.
I would really appreciate any pointers you can give.
Many thanks
My personal favorite method is to use the primitives in
core.atomic with a double or triple buffer. To double buffer,
keep two buffers in an array (data[][] or something) and an
integer index that points to the active buffer, then use
atomicStore to update active buffer index when you want to swap.
Triple buffering can improve runtime performance at the cost of
more memory, and in this case you will need two indices and two
swap methods, one for the producer and another for the consumer.
I use cas with a timed sleep to update the indices in this case.
Take it with a grain of salt, I'm far from a pro, but this has
worked well for me in the past. I can post some example code if
you need it.