Create temporary files for PerfdataWriter in a separate directory.

Fixes #5105
Fix handle leak in the ThreadPool class.

Fixes #5078