1760558 Members
2831 Online
108894 Solutions
New Discussion юеВ

atomic operations

 
Eyal Nadler
New Member

atomic operations

I need to implement few atomic operation (add, inc, compare and swap, swap)using HPUX 11 (9000). Currently I'm using "#pragme _CNX critical_section". Is there another way? Although is seems to be working between threads in the same process, I'm not sure this way is safe when doing operation on objects in shared memory from several different processes.
Thanks, Eyal.
2 REPLIES 2
Carlos Fernandez Riera
Honored Contributor

Re: atomic operations

A. Clay Stephenson
Acclaimed Contributor

Re: atomic operations

Hi Eyal,

No, the CNX #pragma won't do this for you. It does work for what it intended to do - threads within a single process.

I'll point you to two approaches:

1) If all you are doing is integer flag values,
then use semaphores, semop() was made for
this and is atomic. Man semop, semget, and semctl for details. Use ftok() to create a mutually agreed upon and reproducible key. Man ftok for those details.
2) If you are needing to do more complex operations than on operands other than simple integers, then you still use the semaphores as a lock mechanism, do your shared memory stuff, and unlock the semaphore.

This convention will work for threads and processes.

After looking over the man pages, if you are still confused, I can probably give you some simple examples.

Regards, Clay
If it ain't broke, I can fix that.