Performance improvement: Only call pthread_mutex_lock/unlock if there are results to print.