Golang sync. Cond Source Code Analysis

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The main function of cond is to acquire a lock, and the Wait () method waits for a notification to proceed with the next lock release, which controls the appropriate release of the lock, the release frequency, and the wait and notification for goroutine in the concurrency environment.

Sync for Golang 1.9. Cond, the same as the Golang 1.10. Source code location: Sync\cond.go.

Structural body

type Cond struct { noCopy noCopy // noCopy可以嵌入到结构中,在第一次使用后不可复制,使用go vet作为检测使用 // 根据需求初始化不同的锁,如*Mutex 和 *RWMutex L Locker notify notifyList // 通知列表,调用Wait()方法的goroutine会被放入list中,每次唤醒,从这里取出 checker copyChecker // 复制检查,检查cond实例是否被复制 }

Take a look at the wait queue notifylist structure:

type notifyListstruct {uint32uint32uintptrunsafeunsafe.Pointer}

Function

Newcond

Equivalent to the Cond constructor, used to initialize the cond.

The argument is initialized for the locker instance and must be a reference or pointer when passing arguments, such as &sync. mutex{} or new (sync. Mutex), otherwise it will report an exception: Cannot use Lock (type sync. Mutex) as type Sync. Locker in argument to sync. Newcond.
You can think about why it must be a pointer. Know can give me a message to answer.

func NewCond(l Locker) *Condreturn &Cond{L: l}}

Wait

Wait for auto unlock C. L and Pause execution call Goroutine. After resuming execution, wait for lock C. L before returning. Unlike other systems, the wait cannot be returned unless it is awakened by a broadcast or signal.

Because C. When waiting for the first recovery, L is not locked, and the caller usually cannot assume that the condition is correct when the wait is returned. Instead, the caller should wait in the loop:

For ease of understanding, this article is illustrated with a simplified sales model, such as. Figure 1 shows the relationship between the customer, seller, commodity, pricing, order, and other elements such as payment, logistics, and so on.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.