Home > Backend Development > Golang > What are the synchronization mechanisms in go language?

What are the synchronization mechanisms in go language?

青灯夜游
Release: 2022-12-26 17:45:28
Original
4898 people have browsed it

Go synchronization mechanisms include: 1. Channel, focusing on data flow in concurrency issues. Put the flowing data into the channel, and you can use the channel to solve the concurrency; 2. Sync.Mutex, with Lock and Unlock Two methods, the main implementation ideas are reflected in the Lock function; 3. Sync.waitGroup; 4. Sync.Once; 5. Sync.context; 6. Sync.pool; 7. atomic package, which operates on variables.

What are the synchronization mechanisms in go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

The synchronization mechanisms provided by Golang include Mutex and WaitGroup under the sync module and chan provided by the language itself.

1.channel

Overview

Golang tells us in such an obvious way: .

Advantages: The core of channel is data flow. Pay attention to the data flow in concurrency issues. Put the flowing data into the channel, and you can use the channel to solve this concurrency

Problem, and using channels is thread-safe and will not cause data conflicts, which is much easier to use than locks

Disadvantages: Not suitable for scenarios where synchronization is too complex. For example, there is the synchronization waiting problem of multi-coroutine, and there is a deadlock problem, channel deadlock problem:Deadlock problem link

Category

Channel type: unbuffered and buffered type
There are two forms of channel, one is unbuffered. After a thread sends a message to this channel, it will block the current thread until other threads receive it. channel messages. The unbuffered form is as follows:

intChan := make(chan int)
 
带缓冲的channel,是可以指定缓冲的消息数量,当消息数量小于指定值时,不会出现阻塞,超过之后才会阻塞,需要等待其他线程去接收channel处理,带缓冲的形式如下:
 
//3为缓冲数量
intChan := make(chan int, 3)
Copy after login

Example

 type Person struct {
	Name    string
	Age     uint8
	Address Addr
}
 
type Addr struct {
	city     string
	district string
}
 
/*
测试channel传输复杂的Struct数据
 */
func testTranslateStruct() {
	personChan := make(chan Person, 1)
 
	person := Person{"xiaoming", 10, Addr{"shenzhen", "longgang"}}
	personChan <- person
 
	person.Address = Addr{"guangzhou", "huadu"}
	fmt.Printf("src person : %+v \n", person)
 
	newPerson := <-personChan
	fmt.Printf("new person : %+v \n", newPerson)
}
Copy after login

In actual application process, the process of waiting for the channel end signal may not be indefinite, and is usually accompanied by a timer , the timeout is as shown below:

/*
检查channel读写超时,并做超时的处理
 */
func testTimeout() {
	g := make(chan int)
	quit := make(chan bool)
 
	go func() {
		for {
			select {
			case v := <-g:
				fmt.Println(v)
			case <-time.After(time.Second * time.Duration(3)):
				quit <- true
				fmt.Println("超时,通知主线程退出")
				return
			}
		}
	}()
 
	for i := 0; i < 3; i++ {
		g <- i
	}
 
	<-quit
	fmt.Println("收到退出通知,主线程退出")
}
Copy after login

2.Sync.Mutex

Mutex has two methods: Lock and Unlock. The main implementation ideas are reflected in the Lock function middle.

When Lock is executed, there are three situations:

  • No conflict, the current state is set to the locked state through CAS operation;

  • If there is a conflict, start spinning and wait for the lock to be released. If other Goroutine releases the lock during this period, obtain the lock directly; if not released, enter 3;

  • There is a conflict and the spin phase has passed. Call the semacquire function to put the current Goroutine into a waiting state.

When there is no conflict, it is the simplest case; when there is a conflict, spin is performed first for efficiency reasons, because most of the code segments protected by Mutex are very short. It can be obtained after a short spin; if the spin wait is fruitless, the current Goroutine has to wait through the semaphore.

3. Sync.waitGroup

Channel is slightly complicated to use in some synchronization scenarios, whether using multiple channels or channel arrays, as follows:

func coordinateWithChan() {
 sign := make(chan struct{}, 2)
 num := int32(0)
 fmt.Printf("The number: %d [with chan struct{}]\n", num)
 max := int32(10)
 go addNum(&num, 1, max, func() {
  sign <- struct{}{}
 })
 go addNum(&num, 2, max, func() {
  sign <- struct{}{}
 })
 <-sign
 <-sign
}
Copy after login

So Sync.waitGroup is more elegant. Sync.waitGroup is used to wait for the end of a group of goroutines. It is declared in the main Goroutine and sets the number of goroutines to wait for. After each goroutine is executed, Call Done and finally Wait in the main Goroutines. Similar to CountDownLatch or loop barrier in JAVA, and Sync.waitGroup can be reused, providing the following API:

func (wg *WaitGroup) Add(delta int)
func (wg *WaitGroup) Done()
func (wg *WaitGroup) Wait()
Copy after login

However, the use of Sync.waitGroup needs to follow some rules to avoid throwing Panic:
a. The Done method is called incorrectly, resulting in a negative number in the internal count value of waitGroup

b. The Add method is incorrectly called, and when the internal count value in waitGroup reaches 0, the Add method is called, causing the waitGroup internal count value to be called. The goroutine has not been awakened, and a new counting cycle has started.

So when calling, you must follow the following principles:

First unify Add, then concurrently Done, and finally Wait

4. Sync.Once

Sync.once implementation contains an int32-bit flag internally to determine whether the method has been executed. The timing of changing the flag value is after the method is executed. When multiple goroutines are called, the double-check method is used for verification. First, in the absence of synchronization, the flag value is judged. If it is 0, competition is obtained. The mutex lock enters the critical section. At this time, the flag value will be judged here to ensure that the method is actually executed once. The first double-check is for faster judgment, but there are errors. The second check is to correctly determine the status of the flag value at this time.

Usage:

func main() {
    var once sync.Once
    onceBody := func() {
        time.Sleep(3e9)
        fmt.Println("Only once")
    }
    done := make(chan bool)
    for i := 0; i < 10; i++ {
        j := i
        go func(int) {
            once.Do(onceBody)
            fmt.Println(j)
            done <- true
        }(j)
    }
    //给一部分时间保证能够输出完整【方法一】
    //for i := 0; i < 10; i++ {
    //    <-done
    //}

    //给一部分时间保证能够输出完整【方法二】
    <-done
    time.Sleep(3e9)
}
Copy after login

5. Sync.context

Scenario

When multiple batches are required When synchronizing computing tasks for multiple times, or when a one-to-many collaboration process is required

Usage examples

func coordinateWithContext() {
 total := 12
 var num int32
 fmt.Printf("The number: %d [with context.Context]\n", num)
 cxt, cancelFunc := context.WithCancel(context.Background())
 for i := 1; i <= total; i++ {
  go addNum(&num, i, func() {
   if atomic.LoadInt32(&num) == int32(total) {
    cancelFunc()
   }
  })
 }
 <-cxt.Done()
 fmt.Println("End.")
}
Copy after login

Notes

a.如何生成自己的context

通过WithCancel、WithDeadline、WithTimeout和WithValue四个方法从context.Background中派生出自己的子context

注意context.background这个上下文根节点仅仅是一个最基本的支点,它不提供任何额外的功能,也就是说,它既不可以被撤销(cancel),也不能携带任何数据,在使用是必须通过以上4种方法派生出自己的context

b.子context是会继承父context的值

c.撤销消息的传播

撤销消息会按照深度遍历的方式传播给子context(注意因为多routine调用的原因,最终的撤销顺序可能不会是深度遍历的顺序)

,在遍历的过程中,通过WithCancel、WithDeadline、WithTimeout派生的context会被撤销,但是通过WithValue方法派生的context不会被撤销

6. Sync.pool

7.atomic包,针对变量进行操作

我们调用sync/atomic中的几个函数可以对几种简单的类型进行原子操作。这些类型包括int32,int64,uint32,uint64,uintptr,unsafe.Pointer,共6个。这些函数的原子操作共有5种:增或减,比较并交换、载入、存储和交换它们提供了不同的功能,切使用的场景也有区别。

增或减

   顾名思义,原子增或减即可实现对被操作值的增大或减少。因此该操作只能操作数值类型。

   被用于进行增或减的原子操作都是以“Add”为前缀,并后面跟针对具体类型的名称。

//方法源码
func AddUint32(addr *uint32, delta uint32) (new uint32)
Copy after login

栗子:(在原来的基础上加n)

atomic.AddUint32(&addr,n)
Copy after login

栗子:(在原来的基础上加n(n为负数))

atomic.AddUint32(*addr,uint32(int32(n)))
//或
atomic.AddUint32(&addr,^uint32(-n-1))
Copy after login

比较并交换

   比较并交换----Compare And Swap 简称CAS

   他是假设被操作的值未曾被改变(即与旧值相等),并一旦确定这个假设的真实性就立即进行值替换

   如果想安全的并发一些类型的值,我们总是应该优先使用CAS

//方法源码
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
Copy after login

栗子:(如果addr和old相同,就用new代替addr)

ok:=atomic.CompareAndSwapInt32(&addr,old,new)
Copy after login

载入

   如果一个写操作未完成,有一个读操作就已经发生了,这样读操作使很糟糕的。

   为了原子的读取某个值sync/atomic代码包同样为我们提供了一系列的函数。这些函数都以"Load"为前缀,意为载入。

//方法源码
func LoadInt32(addr *int32) (val int32)
Copy after login

栗子

fun addValue(delta int32){
    for{
        v:=atomic.LoadInt32(&addr)
        if atomic.CompareAndSwapInt32(&v,addr,(delta+v)){
            break;
        }
    }
}
Copy after login

存储

   与读操作对应的是写入操作,sync/atomic也提供了与原子的值载入函数相对应的原子的值存储函数。这些函数的名称均以“Store”为前缀

   在原子的存储某个值的过程中,任何cpu都不会进行针对进行同一个值的读或写操作。如果我们把所有针对此值的写操作都改为原子操作,那么就不会出现针对此值的读操作读操作因被并发的进行而读到修改了一半的情况。

   原子操作总会成功,因为他不必关心被操作值的旧值是什么。

//方法源码
func StoreInt32(addr *int32, val int32)
Copy after login

栗子

atomic.StoreInt32(被操作值的指针,新值)
atomic.StoreInt32(&value,newaddr)
Copy after login

交换

   原子交换操作,这类函数的名称都以“Swap”为前缀。

   与CAS不同,交换操作直接赋予新值,不管旧值。

   会返回旧值

//方法源码
func SwapInt32(addr *int32, new int32) (old int32)
Copy after login

栗子

atomic.SwapInt32(被操作值的指针,新值)(返回旧值)
oldval:=atomic.StoreInt32(&value,newaddr)
Copy after login

扩展知识:Sync包简述

1. 什么是Sync包?

Package sync provides basic synchronization primitives such as mutual exclusion locks. Other than the Once and WaitGroup types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication.

Values containing the types defined in this package should not be copied.

这句话大意是说:
Sync包同步提供基本的同步原语,如互斥锁。 除了Once和WaitGroup类型之外,大多数类型都是供低级库例程使用的。 通过Channel和沟通可以更好地完成更高级别的同步。并且此包中的值在使用过后不要拷贝。

从描述中可以看到的是,golang 并不推荐这个包中的大多数并发控制方法,但还是提供了相关方法,主要原因是golang中提倡以共享内存的方式来通信:

不要以共享内存的方式来通信,作为替代,我们应该以通信的手段来共享内存

共享内存的方式使得多线程中的通信变得简单,但是在并发的安全性控制上将变得异常繁琐。
正确性不是我们唯一想要的,我们想要的还有系统的可伸缩性,以及可理解性,我觉得这点非常重要,比如现在广泛使用的Raft算法。

2. 包中的Type

包中主要有: Locker, Cond, Map, Mutex, Once, Pool,
RWMutex, WaitGroup

type Locker interface {
        Lock()
        Unlock()
}
type Cond struct {
        // L is held while observing or changing the condition
        L Locker
}
Copy after login

3. 什么是锁,为什么需要锁?

锁是sync包中的核心,他主要有两个方法,加锁和解锁。
在单线程运行的时候程序是顺序执行的,程序对数据的访问也是:
读取 => 一顿操作(加减乘除之类的) => 写回原地址
但是一旦程序中进行了并发编程,也就是说,某一个函数可能同时被不同的线程执行的时候,以时间为维度会发生以下情况:

What are the synchronization mechanisms in go language?

可以看到的是,A地址的数字被执行了两次自增,若A=5,我们在执行完成后预期的A值是7,但是在这种情况下我们得到的A却是6,bug了~
还有很多类似的并发错误,所以才有锁的引入。若是我们在线程2读取A的值的时候对A进行加锁,让线程2等待,线程1执行完成之后在执行线程2,这样就能够保证数据的正确性。但是正确性不是我们唯一想要的。

4 写更优雅的代码

在很多语言中我们经常为了保证数据安全正确,会在并发的时候对数据加锁

Lock()
doSomething()
Unlock()
Copy after login

Golang在此包中也提供了相关的锁,但是标明了"most are intended for use by low-level library routines" 所以我这里只对 Once and WaitGroup types做简述。

5.Once 对象

Once 是一个可以被多次调用但是只执行一次,若每次调用Do时传入参数f不同,但是只有第一个才会被执行。

func (o *Once) Do(f func())
Copy after login
    var once sync.Once
    onceBody := func() {
        fmt.Println("Only once")
    }
    done := make(chan bool)
    for i := 0; i < 10; i++ {
        go func() {
            once.Do(onceBody)
            done <- true
        }()
    }
    for i := 0; i < 10; i++ {
        <-done
    }
Copy after login

如果你执行这段代码会发现,虽然调用了10次,但是只执行了1次。BTW:这个东西可以用来写单例。

6. WaitGroup

下面是个官方的例子:

var wg sync.WaitGroup
var urls = []string{
        "http://www.golang.org/",
        "http://www.google.com/",
        "http://www.somestupidname.com/",
}
for _, url := range urls {
        // Increment the WaitGroup counter.
        wg.Add(1)
        // Launch a goroutine to fetch the URL.
        go func(url string) {
                // Decrement the counter when the goroutine completes.
                defer wg.Done()
                // Fetch the URL.
                http.Get(url)
        }(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
Copy after login

7. 简述

Golang中高级的并发可以通过channel来实现,这是golang所倡导的,但是go也提供了锁等先关操作。

【相关推荐:Go视频教程编程教学

The above is the detailed content of What are the synchronization mechanisms in go language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template