Avoiding thread divergence