当前Swift 3.0中还没有正式引入基本类型相对应的原子类型。而在macOS以及iOS下,我们可以用系统自带的OSAtomic API进行原子操作处理。但这组API只能在Apple自家平台上使用,我们无法在Linux/FreeBSD中使用,所以我这边封装了顺应C11标准的一组原子类型与原子操作提供给Swift编程语言。


当前在Swift中,如果我们在C语言头文件声明了一个比如atomic_int类型的全局对象,那么该对象在Swift中是无法被导入的,因为atomic_int类型在Swift中无法被转换为它所能识别的相应类型。所以,我这边的思路是将C11标准中支持的原子类型通过结构体进行封装。我在下列代码中封装了3种原子类型,分别为:atomic_int、atomic_bool以及atomic_uint_fast64_t,分别表示int原子类型、布尔原子类型以及无符号64位整数原子类型。我这边建立的项目名为SwiftTest,所以Swift所需要的桥接头文件名为SwiftTest-Bridging-Header.h。下面先给出此头文件的完整源代码:

#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>

/** 用于Swift的AtomicInt原子类型 */
struct AtomicIntSwift {
    volatile atomic_int atom;
};

/**
 * 对原子对象进行初始化
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 指定要给原子对象初始化的值
*/
extern void __attribute__((overloadable)) AtomicSwiftinit(struct AtomicIntSwift* __nonnull pAtom,int value);

/**
 * 对原子对象进行加载操作
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @return 指定原子对象所存储的值
*/
extern int __attribute__((overloadable)) AtomicSwiftLoad(struct AtomicIntSwift* __nonnull pAtom);

/** 
 * 对原子对象进行存储操作
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 要存储到指定原子对象中的值
*/
extern void __attribute__((overloadable)) AtomicSwiftStore(struct AtomicIntSwift* __nonnull pAtom,int value);

/**
 * 将原子对象中存放的值与所指定的基本类型的值进行交换。
 *
 * 也就是说,将基本类型的值存放到原子对象中去,并将原子对象的原始值返回
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern int __attribute__((overloadable)) AtomicSwiftExchange(struct AtomicIntSwift* __nonnull pAtom,int value);

/**
 * 原子比较与交换
 *
 * 将原子对象中的值与expected的值进行比较,如果相同则将desired的值写入到原子对象中,并返回true;
 * 否则,将当前原子对象中的值写入到expected中去,并返回false
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param expected 存放与原子对象进行比较的值的指针。
 *                 该指针一般指向用原子加载操作所得到值的变量,并且不能为空。
 * @param desired 要存入到原子对象中的值
 * @return 如果expected中的值与原子对象中所存放的值相同,则返回true,否则返回false
*/
extern bool __attribute__((overloadable)) AtomicSwiftCAS(struct AtomicIntSwift* __nonnull pAtom,int* __nonnull expected,int desired);

/**
 * 原子加法操作
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern int __attribute__((overloadable)) AtomicSwiftFetchAdd(struct AtomicIntSwift* __nonnull pAtom,int value);

/**
 * 原子减法操作
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern int __attribute__((overloadable)) AtomicSwiftFetchSub(struct AtomicIntSwift* __nonnull pAtom,int value);

/**
 * 原子按位或操作
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern int __attribute__((overloadable)) AtomicSwiftFetchOr(struct AtomicIntSwift* __nonnull pAtom,int value);

/**
 * 原子按位异或操作
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern int __attribute__((overloadable)) AtomicSwiftFetchXor(struct AtomicIntSwift* __nonnull pAtom,int value);

/**
 * 原子按位与操作
 * @param pAtom 指向Swift中AtomicInt类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern int __attribute__((overloadable)) AtomicSwiftFetchAnd(struct AtomicIntSwift* __nonnull pAtom,int value);


/** 用于Swift的AtomicBool原子类型 */
struct AtomicBoolSwift {
    volatile atomic_bool atom;
};

/**
 * 对原子对象进行初始化
 * @param pAtom 指向Swift中AtomicBool类型的原子对象,并且不允许为空
 * @param value 指定要给原子对象初始化的值
*/
extern void __attribute__((overloadable)) AtomicSwiftinit(struct AtomicBoolSwift* __nonnull pAtom,bool value);

/**
 * 对原子对象进行加载操作
 * @param pAtom 指向Swift中AtomicBool类型的原子对象,并且不允许为空
 * @return 指定原子对象所存储的值
*/
extern bool __attribute__((overloadable)) AtomicSwiftLoad(struct AtomicBoolSwift* __nonnull pAtom);

/**
 * 对原子对象进行存储操作
 * @param pAtom 指向Swift中AtomicBool类型的原子对象,并且不允许为空
 * @param value 要存储到指定原子对象中的值
 */
extern void __attribute__((overloadable)) AtomicSwiftStore(struct AtomicBoolSwift* __nonnull pAtom,bool value);

/**
 * 将原子对象中存放的值与所指定的基本类型的值进行交换。
 *
 * 也就是说,将基本类型的值存放到原子对象中去,并将原子对象的原始值返回
 * @param pAtom 指向Swift中AtomicBool类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern bool __attribute__((overloadable)) AtomicSwiftExchange(struct AtomicBoolSwift* __nonnull pAtom,bool value);

/**
 * 原子比较与交换
 *
 * 将原子对象中的值与expected的值进行比较,如果相同则将desired的值写入到原子对象中,并返回true;
 * 否则,将当前原子对象中的值写入到expected中去,并返回false
 * @param pAtom 指向Swift中AtomicBool类型的原子对象,并且不允许为空
 * @param expected 存放与原子对象进行比较的值的指针。
 *                 该指针一般指向用原子加载操作所得到值的变量,并且不能为空。
 * @param desired 要存入到原子对象中的值
 * @return 如果expected中的值与原子对象中所存放的值相同,则返回true,否则返回false
*/
extern bool __attribute__((overloadable)) AtomicSwiftCAS(struct AtomicBoolSwift* __nonnull pAtom,bool* __nonnull expected,bool desired);


/** 用于Swift的AtomicULong原子类型 */
struct AtomicULongSwift {
    volatile atomic_uint_fast64_t atom;
};

/**
 * 对原子对象进行初始化
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 指定要给原子对象初始化的值
*/
extern void __attribute__((overloadable)) AtomicSwiftinit(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

/**
 * 对原子对象进行加载操作
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @return 指定原子对象所存储的值
*/
extern uint64_t __attribute__((overloadable)) AtomicSwiftLoad(struct AtomicULongSwift* __nonnull pAtom);

/**
 * 对原子对象进行存储操作
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 要存储到指定原子对象中的值
*/
extern void __attribute__((overloadable)) AtomicSwiftStore(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

/**
 * 将原子对象中存放的值与所指定的基本类型的值进行交换。
 *
 * 也就是说,将基本类型的值存放到原子对象中去,并将原子对象的原始值返回
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern uint64_t __attribute__((overloadable)) AtomicSwiftExchange(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

/**
 * 原子比较与交换
 *
 * 将原子对象中的值与expected的值进行比较,如果相同则将desired的值写入到原子对象中,并返回true;
 * 否则,将当前原子对象中的值写入到expected中去,并返回false
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param expected 存放与原子对象进行比较的值的指针。
 *                 该指针一般指向用原子加载操作所得到值的变量,并且不能为空。
 * @param desired 要存入到原子对象中的值
 * @return 如果expected中的值与原子对象中所存放的值相同,则返回true,否则返回false
*/
extern bool __attribute__((overloadable)) AtomicSwiftCAS(struct AtomicULongSwift* __nonnull pAtom,uint64_t* __nonnull expected,uint64_t desired);

/**
 * 原子加法操作
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern uint64_t __attribute__((overloadable)) AtomicSwiftFetchAdd(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

/**
 * 原子减法操作
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern uint64_t __attribute__((overloadable)) AtomicSwiftFetchSub(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

/**
 * 原子按位或操作
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern uint64_t __attribute__((overloadable)) AtomicSwiftFetchOr(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

/**
 * 原子按位异或操作
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern uint64_t __attribute__((overloadable)) AtomicSwiftFetchXor(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

/**
 * 原子按位与操作
 * @param pAtom 指向Swift中AtomicULong类型的原子对象,并且不允许为空
 * @param value 指定的基本类型的值
 * @return 原子对象的原始值
*/
extern uint64_t __attribute__((overloadable)) AtomicSwiftFetchAnd(struct AtomicULongSwift* __nonnull pAtom,uint64_t value);

上述代码列出了我们后面在Swift中所需要的C语言底层对C11标准原子操作的封装实现。下面我们可以在C源文件(.c文件)中实现这些声明的函数接口。
#include <stdio.h>
#include "SwiftTest-Bridging-Header.h"

#pragma mark - atomic int

void __attribute__((overloadable)) AtomicSwiftinit(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return;
    
    atomic_init(&pAtom->atom,value);
}

int __attribute__((overloadable)) AtomicSwiftLoad(struct AtomicIntSwift* __nonnull pAtom)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_load(&pAtom->atom);
}

void __attribute__((overloadable)) AtomicSwiftStore(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return;
    
    atomic_store(&pAtom->atom,value);
}

int __attribute__((overloadable)) AtomicSwiftExchange(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_exchange(&pAtom->atom,value);
}

bool __attribute__((overloadable)) AtomicSwiftCAS(struct AtomicIntSwift* __nonnull pAtom,int desired)
{
    if(pAtom == NULL || expected == NULL)
        return false;
    
    return atomic_compare_exchange_weak(&pAtom->atom,expected,desired);
}

int __attribute__((overloadable)) AtomicSwiftFetchAdd(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_add(&pAtom->atom,value);
}

int __attribute__((overloadable)) AtomicSwiftFetchSub(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_sub(&pAtom->atom,value);
}

int __attribute__((overloadable)) AtomicSwiftFetchOr(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_or(&pAtom->atom,value);
}

int __attribute__((overloadable)) AtomicSwiftFetchXor(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_xor(&pAtom->atom,value);
}

int __attribute__((overloadable)) AtomicSwiftFetchAnd(struct AtomicIntSwift* __nonnull pAtom,int value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_and(&pAtom->atom,value);
}


#pragma mark - atomic bool

void __attribute__((overloadable)) AtomicSwiftinit(struct AtomicBoolSwift* __nonnull pAtom,bool value)
{
    if(pAtom == NULL)
        return;
    
    atomic_init(&pAtom->atom,value);
}

bool __attribute__((overloadable)) AtomicSwiftLoad(struct AtomicBoolSwift* __nonnull pAtom)
{
    if(pAtom == NULL)
        return false;
    
    return atomic_load(&pAtom->atom);
}

void __attribute__((overloadable)) AtomicSwiftStore(struct AtomicBoolSwift* __nonnull pAtom,bool value)
{
    if(pAtom == NULL)
        return;
    
    atomic_store(&pAtom->atom,value);
}

bool __attribute__((overloadable)) AtomicSwiftExchange(struct AtomicBoolSwift* __nonnull pAtom,bool value)
{
    if(pAtom == NULL)
        return false;
    
    return atomic_exchange(&pAtom->atom,value);
}

bool __attribute__((overloadable)) AtomicSwiftCAS(struct AtomicBoolSwift* __nonnull pAtom,bool desired)
{
    if(pAtom == NULL || expected == NULL)
        return false;
    
    return atomic_compare_exchange_weak(&pAtom->atom,desired);
}


#pragma mark - atomic uint64_t

void __attribute__((overloadable)) AtomicSwiftinit(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return;
    
    atomic_init(&pAtom->atom,value);
}

uint64_t __attribute__((overloadable)) AtomicSwiftLoad(struct AtomicULongSwift* __nonnull pAtom)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_load(&pAtom->atom);
}

void __attribute__((overloadable)) AtomicSwiftStore(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return;
    
    atomic_store(&pAtom->atom,value);
}

uint64_t __attribute__((overloadable)) AtomicSwiftExchange(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_exchange(&pAtom->atom,value);
}

bool __attribute__((overloadable)) AtomicSwiftCAS(struct AtomicULongSwift* __nonnull pAtom,uint64_t desired)
{
    if(pAtom == NULL || expected == NULL)
        return false;
    
    return atomic_compare_exchange_weak(&pAtom->atom,desired);
}

uint64_t __attribute__((overloadable)) AtomicSwiftFetchAdd(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_add(&pAtom->atom,value);
}

uint64_t __attribute__((overloadable)) AtomicSwiftFetchSub(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_sub(&pAtom->atom,value);
}

uint64_t __attribute__((overloadable)) AtomicSwiftFetchOr(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_or(&pAtom->atom,value);
}

uint64_t __attribute__((overloadable)) AtomicSwiftFetchXor(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_xor(&pAtom->atom,value);
}

uint64_t __attribute__((overloadable)) AtomicSwiftFetchAnd(struct AtomicULongSwift* __nonnull pAtom,uint64_t value)
{
    if(pAtom == NULL)
        return 0;
    
    return atomic_fetch_and(&pAtom->atom,value);
}

我们看到,这个封装实现其实不是很复杂。为了能让原子操作在Swift中更OO化,我在Swift中将每个原子类型抽象为一个结构体。下面我们来看Swift源文件:

/** 所有原子类型所需遵循的原子类型协议 */

public protocol AtomicType {

/** 当前原子类型所对应的基本类型 */

associatedtype RawType

/** 初始化器,对当前原子对象初始化为0值。它不具有原子性 */

init()

/**

初始化器,对当前原子对象初始化为形参指定的值。它不具有原子性

- parameters:

- value: 为当前原子对象初始化的初始值

*/

init(value: RawType)

/**

加载当前原子对象的值,并返回。

- returns:当前原子对象对应的基本类型的值

*/

mutatingfunc load() -> RawType

/**

将一个指定的基本类型值存放到当前原子对象中

- value: 指定要存放的基本类型的值

*/

mutatingfunc store(by value: RawType)

/**

将指定的基本类型的值存入当前原子对象中,并返回存入之前的原子对象的原始值

- value: 指定的基本类型的值

- returns:在将指定值存入之前的原子对象的原始值

*/

mutatingfunc exchange(with value: RawType) -> RawType

/**

原子比较与交换。

当前原子对象的值先与expected的值进行比较,如果相同则将desired的值存入当前原子对象,并返回true

否则将当前原子对象的值存入expected,然后返回false

- expected: 指向基本类型对象的指针。

此基本类型对象在调用该函数前一般会用load方法将当前原子对象的值加载进去。

- desired: 最终所要写入的值

- returns:如果当前原子对象的值与expected中的值相同,那么返回true,否则返回false

*/

mutatingfunc cas(expected: UnsafeMutablePointer<RawType>,desired: RawType) ->Bool

/**

原子加法操作

- operand: 加法操作数

- returns:加法操作之前原子对象的原始值

*/

mutatingfunc fetch_add(operand: RawType) -> RawType

/**

原子减法操作

- operand: 减法操作数

- returns:减法操作之前原子对象的原始值

*/

mutatingfunc fetch_sub(operand: RawType) -> RawType

/**

原子按位或操作

- operand: 按位或操作数

- returns:按位或操作之前原子对象的原始值

*/

mutatingfunc fetch_or(operand: RawType) -> RawType

/**

原子按位异或操作

- operand: 按位异或操作数

- returns:按位异或操作之前原子对象的原始值

*/

mutatingfunc fetch_xor(operand: RawType) -> RawType

/**

原子按位与操作

- operand: 按位与操作数

- returns:按位与操作之前原子对象的原始值

*/

mutatingfunc fetch_and(operand: RawType) -> RawType

}


/** 32位带符号整数原子类型 */

public struct AtomicInt:AtomicType {

privatevar mAtomicValue = AtomicIntSwift()

publictypealias RawType = Int32

publicinit() {

AtomicSwiftinit(&mAtomicValue,0)

}

publicinit(value: Int32) {

AtomicSwiftinit(&mAtomicValue,value)

}

publicmutating func load() ->Int32 {

returnAtomicSwiftLoad(&mAtomicValue)

}

publicmutating func store(by value:Int32) {

AtomicSwiftStore(&mAtomicValue,value)

}

publicmutating func exchange(with value:Int32) -> Int32 {

returnAtomicSwiftExchange(&mAtomicValue,value)

}

publicmutating func cas(expected:UnsafeMutablePointer<Int32>,desired:Int32) -> Bool {

returnAtomicSwiftCAS(&mAtomicValue,desired)

}

publicmutating func fetch_add(operand:Int32) -> Int32 {

returnAtomicSwiftFetchAdd(&mAtomicValue,operand)

}

publicmutating func fetch_sub(operand:Int32) -> Int32 {

returnAtomicSwiftFetchSub(&mAtomicValue,operand)

}

publicmutating func fetch_or(operand:Int32) -> Int32 {

returnAtomicSwiftFetchOr(&mAtomicValue,operand)

}

publicmutating func fetch_xor(operand:Int32) -> Int32 {

returnAtomicSwiftFetchXor(&mAtomicValue,operand)

}

publicmutating func fetch_and(operand:Int32) -> Int32 {

returnAtomicSwiftFetchAnd(&mAtomicValue,operand)

}

}


/** 布尔原子类型 */

public struct AtomicBool:AtomicType {

privatevar mAtomicValue = AtomicBoolSwift()

publictypealias RawType = Bool

ottom:0px; font-size:14px; line-height:normal; font-family:Menlo; color:rgb(187,false)

}

publicinit(value: Bool) {

AtomicSwiftinit(&mAtomicValue,value)

}

publicmutating func load() ->Bool {

returnAtomicSwiftLoad(&mAtomicValue)

}

publicmutating func store(by value:Bool) {

AtomicSwiftStore(&mAtomicValue,value)

}

publicmutating func exchange(with value:Bool) -> Bool {

returnAtomicSwiftExchange(&mAtomicValue,value)

}

publicmutating func cas(expected:UnsafeMutablePointer<Bool>,desired:Bool) -> Bool {

returnAtomicSwiftCAS(&mAtomicValue,desired)

}

publicmutating func fetch_add(operand:Bool) -> Bool {

assertionFailure("Atomic Bool does not support fetch operation!")

returnfalse

}

publicmutating func fetch_sub(operand:Bool) -> Bool {

returnfalse

}

publicmutating func fetch_or(operand:Bool) -> Bool {

returnfalse

}

publicmutating func fetch_xor(operand:Bool) -> Bool {

returnfalse

}

publicmutating func fetch_and(operand:Bool) -> Bool {

returnfalse

}

}


/** 64位无符号整数原子类型 */

public struct AtomicULong:AtomicType {

privatevar mAtomicValue = AtomicULongSwift()

publictypealias RawType = UInt64

ottom:0px; font-size:14px; line-height:normal; font-family:Menlo; color:rgb(187,0)

}

publicinit(value: UInt64) {

AtomicSwiftinit(&mAtomicValue,value)

}

publicmutating func load() ->UInt64 {

returnAtomicSwiftLoad(&mAtomicValue)

}

publicmutating func store(by value:UInt64) {

AtomicSwiftStore(&mAtomicValue,value)

}

publicmutating func exchange(with value:UInt64) -> UInt64 {

returnAtomicSwiftExchange(&mAtomicValue,value)

}

publicmutating func cas(expected:UnsafeMutablePointer<UInt64>,desired:UInt64) -> Bool {

returnAtomicSwiftCAS(&mAtomicValue,desired)

}

publicmutating func fetch_add(operand:UInt64) -> UInt64 {

returnAtomicSwiftFetchAdd(&mAtomicValue,operand)

}

publicmutating func fetch_sub(operand:UInt64) -> UInt64 {

returnAtomicSwiftFetchSub(&mAtomicValue,operand)

}

publicmutating func fetch_or(operand:UInt64) -> UInt64 {

returnAtomicSwiftFetchOr(&mAtomicValue,operand)

}

publicmutating func fetch_xor(operand:UInt64) -> UInt64 {

returnAtomicSwiftFetchXor(&mAtomicValue,operand)

}

publicmutating func fetch_and(operand:UInt64) -> UInt64 {

returnAtomicSwiftFetchAnd(&mAtomicValue,operand)

}

}


然后我们可以对这些原子类型进行测试:

fileprivate func atomInttest() {

var atom =AtomicInt(value: 10)

var value = atom.load()

print("The initial atom value is:\(value)")

atom.store(by: value +100)

value = atom.load()

print("Now,the atom value is:\(value)")

var value2 = atom.exchange(with:-100)

value = atom.load()

print("Before exchange:\(value2),after exchange:\(value)")

value = atom.load()

value += 1// 模拟被读取的值受外界破坏

var bResult = atom.cas(expected: &value,desired:10)

value = atom.load()

print("cas result:\(bResult),value =\(value)")

value = atom.load()

bResult = atom.cas(expected: &value,new value:\(value)")

value = atom.fetch_add(operand:5)

value2 = atom.load()

print("before add:\(value),after add:\(value2)")

value = atom.fetch_sub(operand:5)

value2 = atom.load()

print("before sub:\(value),after sub:\(value2)")

}


fileprivate func atomBooltest() {

var atom =AtomicBool(value: true)

var value = atom.load()

print("The initial atom value is:\(value)")

atom.store(by:false)

value = atom.load()

ottom:0px; font-size:14px; line-height:normal; font-family:Menlo; color:rgb(209,the atom value is:\(value)")

let value2 = atom.exchange(with:true)

value = atom.load()

ottom:0px; font-size:14px; line-height:normal; font-family:Menlo; color:rgb(209,after exchange:\(value)")

value = atom.load()

value = !value// 模拟被读取的值受外界破坏

var bResult = atom.cas(expected: &value,desired:false)

value = atom.load()

ottom:0px; font-size:14px; line-height:normal; font-family:Menlo; color:rgb(209,new value:\(value)")

let _ = atom.fetch_add(operand:false)

}


fileprivate func atomULongtest() {

var atom =AtomicULong(value: 10)

var value = atom.load()

ottom:0px; font-size:14px; line-height:normal; font-family:Menlo; color:rgb(209,the atom value is:\(value)")

var value2 = atom.exchange(with:10000)

value = atom.load()

ottom:0px; font-size:14px; line-height:normal; font-family:Menlo; color:rgb(209,after sub:\(value2)")

}


class ViewController: NSViewController {

overridefunc viewDidLoad() {

super.viewDidLoad()

atomIntTest()

atomULongTest()

atomBoolTest()

}

}


我们在执行最后一个atomBoolTest函数的时候会发生异常,由于原子布尔类型是不支持原子算术逻辑运算操作的,所以在实现中大家也能看到,我使用了assertionFailure函数来处理进入这些实现的方法中。

完整的工程代码可从此链接下载:http://download.csdn.net/detail/zenny_chen/9639566。

Swift中使用C11标准的原子操作的更多相关文章

  1. three.js模拟实现太阳系行星体系功能

    这篇文章主要介绍了three.js模拟实现太阳系行星体系功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  2. HTML5页面无缝闪开的问题及解决方案

    这篇文章主要介绍了HTML5页面无缝闪开方案,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. ios – 为什么,将nil作为参数从Objc C发送到swift类初始化器,用新对象替换nil参数

    除非属性本身被声明为nonnull:

  4. ios – 在Swift中对MKCircle进行子类化

    我想通过添加另一个String属性来继承MKCircle,我们称之为“代码”.这个属性不是可选的和常量的,所以我必须从初始化器设置它,对吧?有没有办法定义一个单一的便利初始化器,在这种情况下需要3个参数?本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. ios – AVAudioPlayer不再使用Swift 2.0/Xcode 7 beta

    对于我的iPhone应用程序中的vartestAudio声明,我在这里收到错误“调用可以抛出,但错误不能从属性初始化程序中抛出”当我转到Xcode7测试版时,就发生了这种情况.如何在Swift2.0中使用此音频剪辑?

  6. ios – 斯威夫特.在初始化所有存储的属性之前在方法调用中使用’self’

    解决方法在初始化所有非可选实例变量之前,您无法在self上调用方法.有几种方法可以解决这个问题.>将属性更改为选项或隐式解包选项(不建议)>使buildCircle()方法静态或只是一个在文件中运行并为所有圆圈调用addSubview()在所有属性初始化并且您调用之后super.init()等等.你必须避免在自己之前打电话给自己class已初始化.

  7. ios – Objective-C警告未找到超类“-init”的指定的初始化程序的方法覆盖

    我在一个应用程序中清理警告,我收到了两次这个警告对于这行代码和这一行我相当新的Objective-C和谷歌这个警告,只是不明白的解决方案我的问题是如何摆脱这些警告?

  8. ios – UICollectionView不能使用UISearchController?

    在WWDC2014年的“AInsideInsidePresentationControllers”中,演示者展示了如何在UITableView中设置UISearchController.他们通过设置searchController的searchBar框架,然后将其设置为tableView的tableHeaderView来实现.不幸的是,UICollectionView没有相当于tableHeade

  9. ios7 – 如何使用默认的IOS映像

    嗨,我是IOS开发的新手.我知道如何在IOS应用程序中使用图像.但是我不知道如何使用默认图像,如开发者站点中提到的共享或书签图标.我想用它们我必须下载这些图像集或那些可用在xcode?

  10. ios – 在词典上引用成员’subscript’

    我正在尝试为类创建一个可用的初始化程序.我的类将使用来自网络请求的输入进行初始化.网络不可靠,我想创建一个初始化器,检查所有属性上的存在,否则它将失败.我试图在这里使用守卫,所以请随时指出方法中的任何明显的错误:守卫self.jobId行无法编译,错误:对成员’下标’的模糊引用关于如何纠正这个错误的任何想法?

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部