Implemented automatic thread synchronization, though it is not fully tested.