New ThreadHandler and ThreadManager classes.

This extracts a lot of OS dependencies from the main code.
Converted example programs to use new ThreadHandler class.
General code cleanup, first version of thread classes.