thread code optimization