Improved close management in threads