Skip to content

Instantly share code, notes, and snippets.

@pjc0247
Last active September 16, 2015 09:02
Show Gist options
  • Save pjc0247/6920e2c1742a60f7c4a9 to your computer and use it in GitHub Desktop.
Save pjc0247/6920e2c1742a60f7c4a9 to your computer and use it in GitHub Desktop.
very simple future implementation for .Net 3.5 or below, without promise
public sealed class Future<T>
{
object m = new object();
T storage;
bool hasValue = false;
public void Produce(T value)
{
if (hasValue == true)
throw new InvalidProgramException ();
lock (m)
{
storage = value;
hasValue = true;
Monitor.Pulse(m);
}
}
public T Acquire()
{
if(hasValue == false)
{
lock (m)
{
while (hasValue == false)
{
Monitor.Wait(m);
}
}
}
return storage;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment