Added a critical section for writing out to the pipeline to ensure that the flushing order of the priority queue is guaranteed.