[threadpool] Fix race condition on threads variable cleanup