improved mutexes and stop writing to output files if profiling was stopped