Skip to content

Instantly share code, notes, and snippets.

@Sasszem
Created February 20, 2017 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sasszem/c211d9a418b4765aea97e4af4e8696a6 to your computer and use it in GitHub Desktop.
Save Sasszem/c211d9a418b4765aea97e4af4e8696a6 to your computer and use it in GitHub Desktop.
Shared variable vs. global. The difference is that shared vars are shared between threads, bud globals don't.
import std.stdio;
import core.thread;
shared int a=5;
int b=5;
void foo()
{writeln(" a=",a);writeln(" b=",b);}
void main()
{
a=7;
b=7;
writeln("Main thread:");
foo();
auto thread = new Thread(&foo);
writeln("Other thread:");
thread.start();
for (int i=0;i<1500; i++){;}//wait some
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment