UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-01-22 01:36 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: atomic_t

I've read a discussion of atomic_t varaibles.As I unerstand it's almost always threadsafe.As for me I use Linux Red Hat 4.0.Can I figure on that type?And i get  this warning:
/usr/include/asm/atomic.h:40:2: warning: #warning Using kernel header in userland program. BAD!
Are there any limits on such atomick_t variables?(It seems no,because that is only atomic operations on a piece of memory)

Offline

#2 2007-01-22 02:44 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: atomic_t

In my atomic.h I found,that atomic_t is a struct with one omember - volatile int.It also has two defines: set(struct->volatile int=values) ans read (struct->volatile int). And the worth thing that there is no function to change the variable and check the value atomically!!:confused:

Offline

#3 2007-01-22 04:33 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: atomic_t

Don't include kernel headers from userspace. At the very least copy the atomic.h header and modify it for your use (which is architecture dependent). Or use libapr from Apache, that one has atomic functions too.

You're looking for functions like atomic_xchg(), atomic_cmpxchg() or atomic_incr_and_return().

Offline

#4 2007-01-22 04:50 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: atomic_t

For the moment I decided to use mutexes,but I'll rember you advice.So I can find the library you mentioned and use that function.Good news).Is the difference in speed using atomic_t much faster(so if operations on atomic_t take one or few instructions of processor with a barier,than how much does it take to use pthread_mutex_lock+.._unlock)?

Offline

#5 2007-01-22 06:14 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: atomic_t

How much faster it is depends on what you do. The current mutex implementation on Linux is very fast, so if you're using it for thread synchronization then don't be stupid and just use them.

But the thing is, if you have a critical section somewhere who's mutex is a bottleneck, it won't help that much to replace it with an atomic op as they have different purposes. Only for very simple things like counters or so, atomic ops can be good, but even then you must be sure to not make wrong assumptions about what guarantees you get when using them.

Only for quite special purposes you should ever consider using atomic operations directly (as they only solve a small part of the problem, and in return bring a lot of complexity). But we talked about this before, so you already know.

Offline

#6 2007-01-23 08:02 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: atomic_t

Yes.So I was going to use atomic_t as counters.However you say mutexes are very fast in linux.So I'll use mutexes.But this possibility is very interresting for me and i'll return to this type later.

Offline

#7 2007-01-25 09:30 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: atomic_t


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

Board footer

Powered by FluxBB