The point is that function is working well as long I got no ML when I let thread to terminate by itself.