Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What's the use of Go Cond?

Shulou Source: shulou.com Published: 2022-06-01 10:51:48 11月16日 Update

This article introduces the relevant knowledge of "what is the use of Go Cond". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Unlike mutexes, the function of conditional variables is not to ensure that only one thread accesses a shared data at a time, but to notify other threads blocked by it when the state of the corresponding shared data changes. Conditional variables are always used in combination with mutexes. Mutexes provide mutex support for access to shared data, while condition variables can notify the relevant thread of changes in the state of the shared data. Here are the main functions:

Func NewCond (l Locker) * Cond: used to create a condition. Pass the pointer of sync.Mutex or sync.RWMutex according to the actual situation. It must be a pointer, otherwise it will copy the invalid func (c * Cond) Broadcast (): wake up all goroutinefunc (c * Cond) Signal () on the condition: wake up the goroutine on the waiting queue randomly. Random way more efficient func (c * Cond) Wait (): suspend the operation of goroutine "what's the use of Go Cond" here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Conditions data variables threads content reality situation pointer more state knowledge change difference practicality achievement of learning next function function dilemma efficiency Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info Apple Shulou Information Linux