Extracted platform-independent Thread interface

Also removed some nasty spin-wait loops.