原子操作 
原子操作是在多线程环境中非常重要的一个概念,原子操作是指一个或一系列的操作,它们作为一个整体来执行,中间不会被任何其他的操作打断。这意味着原子操作要么全部完成,要么全部不完成,不会出现只完成部分操作的情况。
目前 zig 提供了一些内建函数来进行原子操作,并且提供了 std.atomic 命名空间来实现内存排序、原子数据结构。
TIP
该部分内容更适合在单片机或者某些系统级组件开发上使用,常规使用可以使用 std.Thread 命名空间下的类型,包含常规的 Mutex,Condition,ResetEvent,WaitGroup等等。
内建函数 
在讲述下列的内建函数前,我们需要了解一下前置知识:
原子操作的顺序级别:为了实现性能和必要保证之间的平衡,原子性分为六个级别。它们按照强度顺序排列,每个级别都包含上一个级别的所有保证。
关于原子顺序六个级别的具体说明,见 LLVM。
@atomicLoad 
函数原型:
zig
@atomicLoad(
    comptime T: type,
    ptr: *const T,
    comptime ordering: AtomicOrder
) T用于某个类型指针进行原子化的读取值。
@atomicRmw 
函数原型:
zig
@atomicRmw(
    comptime T: type,
    ptr: *T,
    comptime op: AtomicRmwOp,
    operand: T,
    comptime ordering: AtomicOrder
) T用于原子化的修改值并返回修改前的值。
其还支持九种操作符,具体 见此。
@atomicStore 
函数原型:
zig
@atomicStore(
    comptime T: type,
    ptr: *T,
    value: T,
    comptime ordering: AtomicOrder
) void用于对某个类型指针进行原子化的赋值。
@cmpxchgWeak 
函数原型:
zig
@cmpxchgWeak(
    comptime T: type,
    ptr: *T,
    expected_value: T,
    new_value: T,
    success_order: AtomicOrder,
    fail_order: AtomicOrder
) ?T弱原子的比较与交换操作,如果目标指针是给定值,那么赋值为参数的新值,并返回 null,否则仅读取值返回。
@cmpxchgStrong 
函数原型:
zig
@cmpxchgStrong(
    comptime T: type,
    ptr: *T,
    expected_value: T,
    new_value: T,
    success_order: AtomicOrder,
    fail_order: AtomicOrder
) ?T强原子的比较与交换操作,如果目标指针是给定值,那么赋值为参数的新值,并返回 null,否则仅读取值返回。
std.atomic 包 
原子数据结构 
可以使用 std.atomic.Value 包裹某种类型获取到一个原子数据结构。
示例:
zig
const std = @import("std");
const RefCount = struct {
    count: std.atomic.Value(usize),
    dropFn: *const fn (*RefCount) void,
    const RefCount = @This();
    fn ref(rc: *RefCount) void {
        // no synchronization necessary; just updating a counter.
        _ = rc.count.fetchAdd(1, .monotonic);
    }
    fn unref(rc: *RefCount) void {
        // release ensures code before unref() happens-before the
        // count is decremented as dropFn could be called by then.
        if (rc.count.fetchSub(1, .release) == 1) {
            // seeing 1 in the counter means that other unref()s have happened,
            // but it doesn't mean that uses before each unref() are visible.
            // The load acquires the release-sequence created by previous unref()s
            // in order to ensure visibility of uses before dropping.
            _ = rc.count.load(.acquire);
            (rc.dropFn)(rc);
        }
    }
    fn noop(rc: *RefCount) void {
        _ = rc;
    }
};
var ref_count: RefCount = .{
    .count = std.atomic.Value(usize).init(0),
    .dropFn = RefCount.noop,
};
ref_count.ref();
ref_count.unref();TODO: 增加适当的讲解
spinLoopHint 自旋锁 
向处理器发出信号,表明调用者处于忙等待自旋循环内。
示例:
zig
for (0..10) |_| {
    std.atomic.spinLoopHint();
}TODO:增加更多的讲解,例如使用示例,原子级别讲解等!