NVIDIA Interview Question
Software Engineer / DevelopersAs per ARM , you can use swp instruction which will hold/lock the bus and store the value of b in a.
Cant say about x86.
a and b could be grobal variables and multiple processes could be modifying a or b at any time without using a lock.
In such case
.........
b = 1;
<======== (some process changed b here. Let b = 100)
a = b; (we think a =1, but a = 100 now!)
.....................
Use atomic_set(atomic_t *v, int i);
this atomic family is implemented in assembly lanuage since architecture depended. They are atomical.
Single word read and write is atomic on x86 SMP system. So no need for locking the statement a=b. Its atomic by nature. Even if you use spinlock to protect a=b, any thread reading a will either get previous value of a or new value(ie b). And without using any lock for a=b, will also have same effect on any thread reading value of a. So no need to lock a=b statement
- Dinesh December 06, 2011