Call pthread_join for each thread created before disposing chan
Replace sleeps with wait_for_reader/wait_for_writer in tests

This fixes the race conditions occurring in the unit tests.