Global variables ?


I have a program in which each thread calculates a value. I need to store only the smallest value of all. I tried to make all threads read and write into a same buffer cell to find the minimum, but I guess this is a very bad idea since I get a totally random behaviour (which was expectable indeed).

So my question is : is there any way to create a global variable that can be accessed and modified by any thread with some kind of lock mechanism ?


You could use CUDA atomics (
I’d rather store all values in memory and find the smallest using a reduction algorithm such as thrust::reduce (

Thanks, that’s exactly what I was looking for !

Just a quick note:

  • in addition to the Thrust open source project hosted on Github, a production-tested version of Thrust is included in the CUDA Toolkit
  • (