Skip to content

Instantly share code, notes, and snippets.

@ttldtor
Last active August 29, 2015 14:14
Show Gist options
  • Save ttldtor/774a9ae60d307fbcc13f to your computer and use it in GitHub Desktop.
Save ttldtor/774a9ae60d307fbcc13f to your computer and use it in GitHub Desktop.
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