Bug fix on thread exit -- must acquire mutex before waking up and joining.