Fix pthread_detach for already-exited threads. 

This fix handles the case when pthread_detach is
called for threads which have already exited.
This is required to avoid any memory leak