fixed the bug, but it is really slow, need to use condition variables