Last active
August 29, 2015 14:14
-
-
Save ttldtor/774a9ae60d307fbcc13f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import core.atomic; | |
alias atomic_count = long; | |
void increment(ref shared atomic_count a, long b) { | |
atomicOp!"+="(a, b); | |
} | |
unittest { | |
import core.thread; | |
shared atomic_count count = 0; | |
void w() { | |
for (int i = 0; i < 10_000; ++i) { | |
increment(count, 1); | |
} | |
} | |
void w2() { | |
for (int i = 0; i < 10_000; ++i) { | |
increment(count, 10_000); | |
} | |
} | |
auto t1 = new Thread(&w); | |
auto t2 = new Thread(&w2); | |
t1.start(); | |
t2.start(); | |
t1.join(); | |
t2.join(); | |
assert(count == 100_010_000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment