UNIX便携式原子操作 [英] UNIX Portable Atomic Operations

查看:149
本文介绍了UNIX便携式原子操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中有一个(POSIX-)可移植方法,类似于使用pthread的可移植线程吗?

Is there a (POSIX-)portable way in C for atomic variable operations similar to a portable threading with pthread?

原子操作是increment and get ,这意味着没有上下文切换可以干扰操作。在Linux内核空间中,我们必须 atomic_t 类型,在Java中,我们有< a href =http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/package-summary.html =nofollow noreferrer> java.util.concurrent .atomic 包。

Atomic operations are operations like "increment and get" that are executed atomically that means that no context switch can interfere with the operation. In Linux kernel space, we have to atomic_t type, in Java we have the java.util.concurrent.atomic package.

在Linux上,atomic.h文件提供了原子操作,但include是平台相关的 #include< asm-x86_64 / atomic.h> ,但它在Mac OS X上并不可用。

On Linux, the atomic.h file provides atomic operations, but the include is platform dependent e.g. #include <asm-x86_64/atomic.h> and it is not available on Mac OS X in a similar way.

推荐答案

对于任何在未来绊脚石的人,C11原子是现在做这个的最好的方法 - 我相信他们将被包括在GCC 4.9。

For anyone who stumbles upon this in the future, C11 atomics are the best way to do this now - I believe they will be included in GCC 4.9.

这篇关于UNIX便携式原子操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆