Determine when a thread ends

http://ideone.com/WrFrXb