Last active
September 16, 2015 09:02
-
-
Save pjc0247/6920e2c1742a60f7c4a9 to your computer and use it in GitHub Desktop.
very simple future implementation for .Net 3.5 or below, without promise
This file contains 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 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