当前位置:网站首页>Go language - lock operation
Go language - lock operation
2022-06-25 16:03:00 【Crying while learning】
summary
sync Package document :Package sync - The Go Programming Language
sync Packages provide basic synchronization operations , Such as mutual exclusion lock . except Once and WaitGroup outside , Most types are used by low-level library routines . The more advanced synchronization is better through channel And communication .
The mutex Mutex
Mutexes are more violent , When one goroutine After obtaining the mutex , Other goroutine You can only wait for the lock to be released .
When using mutex , You must unlock the resource after the operation , Otherwise the program will deadlock . It is recommended to use defer Statement to unlock .
// Define mutex objects
var mutex_name sync.Mutex
// locked
mutex_name.Lock()
// Unlock
mutex_name.Unlock()The same is the problem of ticket sales at the railway station , Let's take a look at the effect of solving the critical resource problem through mutual exclusion
// Ticket allowance
var tickets = 5
// Define synchronization waiting groups
var wg sync.WaitGroup
// Define mutex objects
var mutex sync.Mutex
func main() {
wg.Add(3)
go saleTickets("1 Ticket booth No ")
go saleTickets("2 Ticket booth No ")
go saleTickets("3 Ticket booth No ")
wg.Wait()
}
func saleTickets(name string) {
for {
mutex.Lock()
if tickets > 0 {
fmt.Printf(" Ticket remaining :%v, %v Sell out \n", tickets, name)
tickets--
time.Sleep(1 * time.Second)
} else {
mutex.Unlock()
fmt.Println(" The tickets have been sold out ...")
wg.Done()
break
}
mutex.Unlock()
}
}
Read-write lock RWMutex
The difference between a read-write lock and an ordinary mutex lock is , It is a kind of mutex for read and write operations .
Multiple read operations are allowed at the same time , But only one write operation is allowed at a time . And during a write operation , Read operations are also not allowed . So , Read / write locks can be concurrent for read operations , But for write operations, they are completely mutually exclusive .
// Define the read / write lock object
var rwmutex_name sync.RWMutex
// Read the lock
rwmutex_name.RLock
// Read unlock
rwmutex_name.RUnlock
// Write lock
rwmutex_name.Lock
// Write unlock
rwmutex_name.UnlockWe will not show the example of read-write lock , Because the effect should not be different from the example shown above .
The problem actually solved with write locks is , The use of mutexes reduces concurrency . Because mutexes block both read and write operations , The program has changed from concurrent to serial .
In fact, for read operations that do not modify critical resources , We don't need to block ; Only write operations that need to be modified for critical resources , We just need to block . So read-write locking is to improve the concurrency of programs when locking .
边栏推荐
- Jz-065 path in matrix
- What is the NFT digital collection?
- 转换Cifar10数据集
- Sleep formula: how to cure bad sleep?
- What is OA
- Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
- Tensorflow loading cifar10 dataset
- 不要小看了积分商城,它的作用可以很大!
- Linux-MySQL数据库之高级SQL 语句一
- Popular cross domain
猜你喜欢

TFIDF and BM25

商城风格也可以很多变,DIY了解一下!

GridLayout evenly allocate space
How to debug grpc by postman

Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官

Understand the execution sequence of try catch finally in one diagram
Practice of geospatial data in Nepal graph

商城风格也可以很多变,DIY 了解一下!
Open the box to experience rust, come on!!!

AutoK3s v0.5.0 发布 延续简约和友好
随机推荐
VectorDraw Developer Framework 10.1001 Crack
Differences between = = and = = = in JS (detailed explanation)
深度学习 pytorch cifar10数据集训练「建议收藏」
报错:homebrew-core is a shallow clone
Client development (electron) system level API usage
Tensorflow loading cifar10 dataset
How to debug grpc by postman
Message format of Modbus (PLC)
Power representation in go language
Rapport de la main - d'oeuvre du Conseil de développement de l'aecg air32f103cbt6
Classic deadlock scenario of multithreading and its solution (philosopher dining problem)
分享自己平时使用的socket多客户端通信的代码技术点和软件使用
MySQL installation tutorial
Sword finger offer 03 Duplicate number in array
一行代码可以做什么?
Golang regular regexp package uses -05- extend expand(), cut split() according to the rule
Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官
Advanced SQL statement 1 of Linux MySQL database
Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
Mark the same items in the Li list in red