Synchronize profile printing.

This avoids interleaving/garbling of profiles from multiple threads.
Merge pull request #735 from grddev/threadsafe_profile

Synchronize profile printing.