Last active
January 9, 2019 13:17
-
-
Save kevingosse/f21bf56cb563ccadead3c5531ee0b5bd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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