atomic operations

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.
 
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).
 
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?
 
> 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 :)
 
Back
Top