Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Last active January 9, 2019 13:17
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 kevingosse/f21bf56cb563ccadead3c5531ee0b5bd to your computer and use it in GitHub Desktop.
Save kevingosse/f21bf56cb563ccadead3c5531ee0b5bd to your computer and use it in GitHub Desktop.
public class PhantomReference
{
private readonly ReferenceQueue _queue;
protected PhantomReference(ReferenceQueue queue)
{
_queue = queue;
}
~PhantomReference()
{
_queue.Notify(this);
}
}
public class ReferenceQueue
{
private readonly ConcurrentQueue<PhantomReference> _references;
public ReferenceQueue()
{
_references = new ConcurrentQueue<PhantomReference>();
}
public PhantomReference Poll()
{
return _references.TryDequeue(out var value) ? value : null;
}
internal void Notify(PhantomReference value)
{
_references.Enqueue(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment