How to stop Linux threads cleanly