原子操作 怎么弄?

xengine-qyt 2011-09-24 03:35:28
就是每次可以自加1或者减一的原子操作。。。

linux下是怎么做的?

我查了下 atomic_sub 好像可以,但是找不到定义 不知道是哪儿来的。。。。
...全文
251 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ma100 2011-09-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhuzelu 的回复:]
原子操作就是让一次操作不被打断吧
这可能有好多方式可以实现
信号灯
互斥锁
信号量
都可以
[/Quote]
你说的不是原子操作,原子操作不仅不会被本进程的其他部分打断,而且在整个系统的所有程序都不打断才行
poor_coder 2011-09-26
  • 打赏
  • 举报
回复
用户态一般没有整数原子操作的说法,这些功能都在在内核使用。

linux-2.6.xx/include/asm-i386/atomic.h:


/*
* Make sure gcc doesn't try to be clever and move things around
* on us. We need to use _exactly_ the address the user gave us,
* not some alias that contains the same information.
*/
typedef struct { volatile int counter; } atomic_t;

#define ATOMIC_INIT(i) { (i) }

/**
* atomic_read - read atomic variable
* @v: pointer of type atomic_t
*
* Atomically reads the value of @v.
*/
#define atomic_read(v) ((v)->counter)

/**
* atomic_set - set atomic variable
* @v: pointer of type atomic_t
* @i: required value
*
* Atomically sets the value of @v to @i.
*/
#define atomic_set(v,i) (((v)->counter) = (i))

/**
* atomic_add - add integer to atomic variable
* @i: integer value to add
* @v: pointer of type atomic_t
*
* Atomically adds @i to @v.
*/
static __inline__ void atomic_add(int i, atomic_t *v)
{
__asm__ __volatile__(
LOCK "addl %1,%0"
:"=m" (v->counter)
:"ir" (i), "m" (v->counter));
}

/**
* atomic_sub - subtract the atomic variable
* @i: integer value to subtract
* @v: pointer of type atomic_t
*
* Atomically subtracts @i from @v.
*/
static __inline__ void atomic_sub(int i, atomic_t *v)
{
__asm__ __volatile__(
LOCK "subl %1,%0"
:"=m" (v->counter)
:"ir" (i), "m" (v->counter));
}

/**
* atomic_sub_and_test - subtract value from variable and test result
* @i: integer value to subtract
* @v: pointer of type atomic_t
*
* Atomically subtracts @i from @v and returns
* true if the result is zero, or false for all
* other cases.
*/
static __inline__ int atomic_sub_and_test(int i, atomic_t *v)
{
unsigned char c;

__asm__ __volatile__(
LOCK "subl %2,%0; sete %1"
:"=m" (v->counter), "=qm" (c)
:"ir" (i), "m" (v->counter) : "memory");
return c;
}

/**
* atomic_inc - increment atomic variable
* @v: pointer of type atomic_t
*
* Atomically increments @v by 1.
*/
static __inline__ void atomic_inc(atomic_t *v)
{
__asm__ __volatile__(
LOCK "incl %0"
:"=m" (v->counter)
:"m" (v->counter));
}

/**
* atomic_dec - decrement atomic variable
* @v: pointer of type atomic_t
*
* Atomically decrements @v by 1.
*/
static __inline__ void atomic_dec(atomic_t *v)
{
__asm__ __volatile__(
LOCK "decl %0"
:"=m" (v->counter)
:"m" (v->counter));
}

... ...
qq120848369 2011-09-26
  • 打赏
  • 举报
回复
原子操作和同步还是不一样的,真正的原子操作还是得操作系统支持的,同步自己做互斥就可以了。
IterZebra 2011-09-26
  • 打赏
  • 举报
回复
你可以使用嵌入汇编代码的方式,或者写入编译指示,来形成简单的原子操作,例如自加、自减。
xengine-qyt 2011-09-26
  • 打赏
  • 举报
回复
linux 不提供本身的原子操作函数? 而是要靠其他技术来实现?
lvyinghong 2011-09-26
  • 打赏
  • 举报
回复

/**
* atomic_sub - subtract the atomic variable
* @i: integer value to subtract
* @v: pointer of type atomic_t
*
* Atomically subtracts @i from @v.
*/
static __inline__ void atomic_sub(int i, atomic_t *v)
{
__asm__ __volatile__(
LOCK "subl %1,%0"
:"=m" (v->counter)
:"ir" (i), "m" (v->counter));
}





很容易找阿
lvyinghong 2011-09-26
  • 打赏
  • 举报
回复
用了 汇编里面 .lock 前缀

就是 指定锁定总线吧,

普通的 减 sub

原子的就是 .lock sub

这样吧,cpu提供的保证
smart_fish8 2011-09-25
  • 打赏
  • 举报
回复
原子操作就是让一次操作不被打断吧
这可能有好多方式可以实现
信号灯
互斥锁
信号量
都可以
justkk 2011-09-24
  • 打赏
  • 举报
回复
信号灯
System V版本的,或posix版本的都可以
帅得不敢出门 2011-09-24
  • 打赏
  • 举报
回复
3代表的是man中的3

帅得不敢出门 2011-09-24
  • 打赏
  • 举报
回复
#include <semaphore.h>
sem_init
sem_destroy(3), sem_post(3), sem_wait(3)

sem_open
sem_close(3), sem_getvalue(3), sem_post(3), sem_unlink(3), sem_wait(3)

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧