Added locks to prevent concurrency problems in the parallel API.