This is somewhat copy paste from an old script. Using stringstream and then just 1 cout << ss.str() prevents multiple threads printing from mangling each other's output.