Improve thread pool implementation

thpool_destroy() can now wait for threads to empty the job queue
completely before joining them.