Simultaneous write Multiple threads writing to the same memory location?


is it possible to lock part of the code executed on the device

such that multiple threads writing on the same memory cell

do not “disturb” each other?

By the way, the value the threads will be writing is the same

  • maybe I do not need to lock this part of the code?

For example:

__global__ void comp(int* array)


	// Do some computation

	// ...

	// Compute an index (it will happen that more than

	// one thread computes the same 'id')

	int id = compId();

	// Should I lock somehow the following line before writing to 'array'?

	array[id] = 1;

	// ...


‘array’ will be read on the host after the method above completes.

Thanks a lot.


you want to use atomic operations. check the reference in the programming guide.

If you are sure the value will always be the same then you don’t need to do anything.

Otherwise use atomic, there is a example project called “simpleAtomicIntrinsics” in the SDK