wait for detached threads before destroying to prevent segfaults