atomic_ops vs mutex

View: New views
3 Messages — Rating Filter:   Alert me  

atomic_ops vs mutex

by raymond.meyer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The following is from the atomic_ops(3) man page:

Performance
Because atomic memory operations require expensive
synchronization at the hardware level, applications should take care to
minimize their use.  In certain cases, it may be more appropriate to
use a mutex, especially if more than one memory location will be
modified.

I have two questions:

1. Are all atomic_ops functions, or some of them macros?

2. In what cases would it be better to use atomic_ops, instead of
locking and unlocking mutex?

Re: atomic_ops vs mutex

by Adam Hamsik-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
On Nov,Saturday 7 2009, at 4:21 AM, raymond.meyer@... wrote:

> The following is from the atomic_ops(3) man page:
>
> Performance
> Because atomic memory operations require expensive
> synchronization at the hardware level, applications should take care  
> to
> minimize their use.  In certain cases, it may be more appropriate to
> use a mutex, especially if more than one memory location will be
> modified.
>
> I have two questions:
>
> 1. Are all atomic_ops functions, or some of them macros?
>
> 2. In what cases would it be better to use atomic_ops, instead of
> locking and unlocking mutex?

I think that in case when you want to use atomic_ops for 3-4 memory  
locations you should use mutex.



Regards

Adam.


Re: atomic_ops vs mutex

by Matthias Scheler-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Nov 07, 2009 at 03:21:48AM +0000, raymond.meyer@... wrote:
> I have two questions:
>
> 1. Are all atomic_ops functions, or some of them macros?

They are functions as documented in the manual page (at least as far
as I can tell).

> 2. In what cases would it be better to use atomic_ops, instead of
>    locking and unlocking mutex?

It depends what your application is doing. Locking and unlocking a
mutex requires (at least) two atomic operations. If you e.g. want
to update a single number an atomic operation will be cheaper.
If you want to update at least three a mutex will cause less
overhead. If you need to keep multiple variables consistent (e.g.
the head and tail pointer of a list) you will need a mutex.

        Kind regards

--
Matthias Scheler                                  http://zhadum.org.uk/