以前文章中介绍的互斥锁虽然可以保证同串行化,可是却保证不了执行过程当中的中断。
要么成功、要么失败,没有中断的状况,咱们叫它叫原子性,这种由硬件 CPU 提供支持的特性,是很是可靠的。git
百度百科上关于原子操做的介绍。github
由 sync/atomic 包提供操做支持。编程
实现累加并发
func TestDemo1(t *testing.T) { var counter int64 = 0 for i := 0; i < 100; i++ { go func() { atomic.AddInt64(&counter, 1) }() } time.Sleep(2 * time.Second) log.Println("counter:", atomic.LoadInt64(&counter)) }
结果性能
=== RUN TestDemo1 2020/10/11 00:24:56 counter: 100 --- PASS: TestDemo1 (2.00s) PASS
对于作减法,是没有直接提供的方法的,而 Add(-1)这种是不能对 uint 类型使用的,能够经过补码的方式实现学习
func TestDemo2(t *testing.T) { var counter uint64 = 100 for i := 0; i < 100; i++ { go func() { atomic.AddUint64(&counter, ^uint64(-(-1)-1)) }() } time.Sleep(2 * time.Second) log.Println("counter:", atomic.LoadUint64(&counter)) }
结果ui
=== RUN TestDemo2 2020/10/11 00:32:05 counter: 0 --- PASS: TestDemo2 (2.00s) PASS
并发编程中,在没有使用互斥锁的前提下,对共享数据先取出作判断,再根据判断的结果作后续操做,必然是会出问题的,使用 CAS 能够避免这种问题。atom
func TestDemo3(t *testing.T) { var first int64 = 0 for i := 1; i <= 10000; i++ { go func(i int) { if atomic.CompareAndSwapInt64(&first, 0, int64(i)) { log.Println("抢先运行的是 goroutine", i) } }(i) } time.Sleep(2 * time.Second) log.Println("num:", atomic.LoadInt64(&first)) }
结果code
=== RUN TestDemo3 2020/10/11 00:42:10 抢先运行的是 goroutine 3 2020/10/11 00:42:12 num: 3 --- PASS: TestDemo3 (2.01s) PASS
加载操做在进行时只会有一个,不会有其它的读写操做同时进行。队列
func TestDemo4(t *testing.T) { var counter int64 = 0 for i := 0; i < 100; i++ { go func() { atomic.AddInt64(&counter, 1) log.Println("counter:", atomic.LoadInt64(&counter)) }() } time.Sleep(2 * time.Second) }
存储操做在进行时只会有一个,不会有其它的读写操做同时进行。
func TestDemo5(t *testing.T) { var counter int64 = 0 for i := 0; i < 10; i++ { go func(i int) { atomic.StoreInt64(&counter, int64(i)) log.Println("counter:", atomic.LoadInt64(&counter)) }(i) } time.Sleep(2 * time.Second) }
swap 方法返回被替换以前的旧值。
func TestDemo6(t *testing.T) { var counter int64 = 0 for i := 0; i < 10; i++ { go func(i int) { log.Println("counter old:", atomic.SwapInt64(&counter, int64(i))) }(i) } time.Sleep(2 * time.Second) }
结果
=== RUN TestDemo6 2020/10/11 00:43:36 counter old: 0 2020/10/11 00:43:36 counter old: 9 2020/10/11 00:43:36 counter old: 5 2020/10/11 00:43:36 counter old: 1 2020/10/11 00:43:36 counter old: 2 2020/10/11 00:43:36 counter old: 3 2020/10/11 00:43:36 counter old: 6 2020/10/11 00:43:36 counter old: 4 2020/10/11 00:43:36 counter old: 7 2020/10/11 00:43:36 counter old: 0 --- PASS: TestDemo6 (2.00s) PASS
value是一个结构体,内部值定义为 interface{},因此它是能够接受任何类型的值。
第一次赋值的时候,原子值的类型就确认了,后面不能赋值其它类型的值。
func TestDemo7(t *testing.T) { var value atomic.Value var counter uint64 = 1 value.Store(counter) log.Println("counter:", value.Load()) value.Store(uint64(10)) log.Println("counter:", value.Load()) value.Store(100) // 引起 panic log.Println("counter:", value.Load()) time.Sleep(2 * time.Second) }
结果
=== RUN TestDemo7 2020/10/11 10:14:58 counter: 0 2020/10/11 10:14:58 counter: 10 --- FAIL: TestDemo7 (0.00s) panic: sync/atomic: store of inconsistently typed value into Value [recovered] panic: sync/atomic: store of inconsistently typed value into Value ... Process finished with exit code 1
此处暂时先介绍一下,后面有机会出文章再一块儿学习进步。
放弃互斥锁,采用原子操做,常见方法有如下几种:
可使用例如上面介绍的 Add 方法。
单生产者、单消费者能够作到免锁访问环形缓冲区(Ring Buffer)。
好比,Linux kernel 中的 kfifo 的实现。
新旧副本切换机制,对于旧副本能够采用延迟释放的作法。
如无锁栈,无锁队列等待