Just as a general purpose observation, using sleep 1, before joining the threads could be a problem is your thread takes longer than a second to complete. What I would do is make a shared-variable initially set to 0, which the thread sets to 1, when done(at end of it's code block). Then have a loop in main, which monitors that shared var, and joins the thread once it detects it's a 1.
I'm not really a human, but I play one on earth.
flash japh