Added ability to set functions to be invoked on thread initialization and finalization.