Add lock while writing to global cost_factors