Lost output in multithreaded program