Synchronized increment to prevent threading issues.