Example usage of the core.sync classes

Vlad Levenfeld via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 3 14:10:48 PST 2015


On Saturday, 3 January 2015 at 15:44:16 UTC, Matt wrote:
> Right, I've been looking at core.atomic, but it has very little 
> documentation, and it's territory I haven't explored, yet. Any 
> chance of some pointers along the way?

Could you be more specific about what you need help 
understanding? You can implement a very simple double buffer like 
so:

   synchronized final class DoubleBuffer (T) {
     T[][2] data;
     uint write_index;

     shared void swap () {
       atomicStore (write_index, (write_index + 1) % 2);
       (cast()this).buffer[write_index].clear;
     }

     shared @property write_buffer () {
       return (cast()this).buffer[write_index][];
     }
     shared @property read_buffer () {
       return (cast()this).buffer[(write_index + 1) % 2][];
     }
   }

Just append to write_buffer, call swap, then read from 
read_buffer. Make sure you declare the instance shared. 
Benjamin's post links to a more robust and realistic 
implementation.


More information about the Digitalmars-d-learn mailing list