Move multithreading out of the template function to reduce binary size.