added multithread support to important cpp functions