Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Created August 30, 2015 16:51
Show Gist options
  • Save PetarKirov/14076777dff7d879d659 to your computer and use it in GitHub Desktop.
Save PetarKirov/14076777dff7d879d659 to your computer and use it in GitHub Desktop.
import core.stdc.stdio : printf;
import std.stdio : writefln;
import gc.proxy : _gc;
import core.exception : InvalidMemoryOperationError;
class Resource
{
int id = id.min;
this(int i) { this.id = i; }
~this()
{
writefln("Entering #%d Resource.dtor().\n", this.id);
if (_gc.gcx.running)
printf("A GC collection is running!\n");
if (_gc.gcLock.tryLock())
{
scope (exit) _gc.gcLock.unlock();
printf("The GC is ours to command!\n");
}
else
printf("The GC lock is taken!\n");
writefln("Exiting #%d Resource.dtor().\n", this.id);
}
}
void main()
{
writefln("Entering main().\n");
auto r1 = new Resource(1);
destroy(r1);
auto r2 = new Resource(2);
writefln("Exiting main().\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment