Fixes tests that left threads behind after run

introduced a simple tracking method that can be used for labelling
threads, only active if assertions are enabled.
Merge pull request #3259 from tinwelint/2.2-clean-test-litter

[2.2] Fixes tests that left threads behind after run