fix bug in mulit-threaded applications