Implementing multithreading policy templates in D?
Brian Price
blprice61 at yahoo.com
Sat Jun 7 12:33:13 PDT 2008
Greetings,
While implementing a design I ran into a need for multiple implementations with different threading policies. It looked to me as if porting Loki's Threading Model policies over to D would be just the ticket. Unfortunately there's no mutex-like class in Phobos that I can find and Object's monitor is not exposed so no way to acquire/release on it other than through synchronized.
Without a mutex like object having aquire/release semantics, I'd have to litter my code with a bunch of compile time conditionals instead of using a RAII Lock object. Such a Lock object could be provided via template argument allowing different threading policies for different instantiations. In the 'non-thread-safe' scenario the Lock object would just be an empty object that (I assume) the compiler would optimize away.
Since I couldn't figure out how to make such a Lock object using the synchronized statement, I tried rolling my own mutex on top of D 2.014's standard library (Phobos) and language primitives. Since volatile seems to be deprecated, my design uses two synchronization objects through synchronized statements and requires a waitable object that can be signaled by the releasing thread to avoid busy-wait.
At this point I hit brick wall #2 (#1 being the absence of a mutex). The only waitable object I can find in the standard library is Thread and the only event you can wait on is its death. Up to this point the pure D standard mutex implementation was doable (though undoubtedly less efficient than native implementations). Creating a std.Thread derived class to be used as a one shot wait object pushed the design into the yes it will work but it's completely absurd camp.
Having used about every 'mainstream' language over the past twenty odd years, I figure either I'm missing something huge and need to learn an entirely new approach or there's something missing from the standard library. So I'm left with three questions:
Did I miss something in the docs/std lib code?
Is there a way to implement flexible threading policies using synchronized statements?
What are the chances we'll see Object sporting wait/notify methods or lock/unlock methods in a future release?
Thanks,
Brian
More information about the Digitalmars-d
mailing list