Threaded one-time variable assignment with multiple listeners in C# .NET

I have a class of type Variable which contains an field of type Value, which initially is unset (null) but will be set at some point in the future. Multiple threads may have access to one Variable-object. A method with the signature Variable.Set(Value v) is called exactly once and sets the variable. Many other threads should be able to call Variable.Wait() (both before and after the Variable is set) which:



  • if the Variable is already set, should return immediately.

  • if the Variable is not set, should wait (efficiently) for it to be set, and then return.


As I am looking at the API, I am sure that there is some obvious synchronization primitive around – monitors, events, wait handles or similar – but I need help figuring out the simplest, most efficient solution as I am learning about parallelism in .NET.