what happens if the synchronization is only visible for part of threads?
In general, this is an illegal use-case for __syncthreads()
and this is covered in the documentation:
__syncthreads() is allowed in conditional code but only if the conditional evaluates identically across the entire thread block, otherwise the code execution is likely to hang or produce unintended side effects.
So the result of your proposed usage inside the if
statement would be undefined (assuming some threads have a tid
that is greater than or equal to size
).
There is additional discussion here and your question is arguably a duplicate of that one. I don't wish to quibble about whether or not questions are actually duplicates (dropped threads vs. excluded threads), so I've provided this CW answer linked to that one.
If someone wants to mark this is a duplicate I will delete this answer.