Example usage of the core.sync classes

Matt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 3 17:02:06 PST 2015


On Saturday, 3 January 2015 at 22:10:49 UTC, Vlad Levenfeld wrote:
> 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.

What I mean is that I don't understand what atomicStore, 
atomicLoad, etc. actually DO, although in the case of the two 
mentioned, I can hazard a pretty good guess. The documentation 
doesn't exist to tell me how to use the functions found in the 
module. I've never used any atomic functions in any language 
before.

However, thank you for the simple double buffered example, I do 
appreciate it. How would another thread gain access to the 
instance in use, though? Am I able to pass references to 
instances of this class via events? 'synchronized' is also 
something I'm clueless about, and again the D documentation 
doesn't help, as the section in the class documentation basically 
says "synchronized classes are made of synchronized functions", 
without explaining what a synchronized function IS. The function 
section makes no mention of the synchronized keyword, either.

Sorry, it feels like there's a load of stuff in the library and 
language to make multithreading easier, but with little to no 
explanation, so it feels like you're expected to already know it, 
unless I'm looking in the wrong places for my explanations. If I 
am, please do give me a kick, and point me in the right direction.


More information about the Digitalmars-d-learn mailing list