# atomic operations



## vtenev (Nov 26, 2009)

Hello everyone,

I am interested in employing atomic operations to update certain values in a multithreaded application (using atomic_set_<type>).

I've been reading the specs for machine/atomic.h, and it appears that all of the functions act on unsigned integer datatypes.

Can I use atomic_set_64 to update the value of a double simply by casting the value to unsigned int? I assume it does strict byte duplication?

Thanks.


----------



## melkov (Dec 1, 2009)

Yes these functions will work correctly with casted double's (like _*(uint64_t*)&d_) unless their nature is misunderstood.

Here are 3 possible problems:
1) Cast to correct type (int is 32 bit btw, double is 64-bit);
2) Comply with c++ aliasing rules if you are writing in c++ not c;
3) Be attentive about what functions do. atomic_set_64 is a bitwise OR which is not really useful with double's (see comment of atomic_set_long), what you need is most likely a loop with atomic_cmpset_64 (see comment above atomic_cmpset_int).


----------



## vtenev (Dec 1, 2009)

Thanks for the reply! Indeed you're right it's a bitwise OR, I misread the manual page.

Let me see if I understand.

Supposing I need to atomically set only one double, I can use atomic_store_64.

I'm guessing your suggestion of using atomic_cmpset_64 is for when I need to do multiple all-or-nothing atomic stores? Am I understanding correctly? And what are these comments that you are referring to?


----------



## melkov (Dec 1, 2009)

_> I'm guessing your suggestion of using atomic_cmpset_64 is for when I need to do multiple all-or-nothing atomic stores?_
No, I was about simpler operations on doubles, like atomic *a += b.

_> And what are these comments that you are referring to?_
Comments in machine/atomic.h


----------

