Example usage of the core.sync classes

Vlad Levenfeld via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 3 18:20:19 PST 2015


On Sunday, 4 January 2015 at 01:02:07 UTC, Matt wrote:
> 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.

Atomic operations are guaranteed to happen in "one step". For 
example, x += 1 will load the value of x into a register, 
increment it by 1, then write the value back to memory, while 
atomicStore (x, x+1) will cause all three steps to be executed as 
if they were instantaneous. This prevents a thread writing to an 
address that another thread just read from, causing that thread's 
view of the data to be silently outdated (aka a race condition, 
because the final value of x depends on which thread writes 
first).

> 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?

You can if they're shared. You can pass the reference via 
message, or have the reference exist at a location both threads 
have access to. The two @property functions in the example are 
meant to be used by the writing and reading thread, respectively.

I go about the swap by letting the reader thread message the 
writer to inform it that it is ready for more data, at which 
point the writer thread is free to call .swap () once it is done 
writing (though you can do it the opposite way if you want).

> '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.

A synchronized class has a hidden mutex, and any thread that 
calls a member method acquires the lock. The synchronized keyword 
isn't limited to classes, though - you can find a detailed 
description of synchronized's various uses here: 
http://ddili.org/ders/d.en/concurrency_shared.html

> 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.

Yeah, I think the docs are meant to be a reference if you already 
have in mind what you're looking for, and just need to know how 
to wire it up.

I recommend Ali's book (linked in the previous paragraph) in 
general as its the most comprehensive and up-to-date explanation 
of the language as a whole that I've yet seen (and its free).


More information about the Digitalmars-d-learn mailing list