当前位置:网站首页>Go language - what is critical resource security?
Go language - what is critical resource security?
2022-06-25 16:03:00 【Crying while learning】
Critical resource security issues
Critical resources : In concurrent programming , Can be used by multiple processes / Threads / Resources jointly accessed by the collaboration process .
If the critical resources are not handled properly in concurrent programming , It often leads to inconsistent data .
Ticket sales at the railway station demo, To demonstrate critical resource security issues
Realization 3 Tickets are sold at the same time
// Ticket allowance
var tickets = 5
// Define synchronization waiting groups
var wg sync.WaitGroup
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 {
if tickets > 0 {
fmt.Printf(" Ticket remaining :%v, %v Sell out \n", tickets, name)
tickets--
} else {
fmt.Println(" The tickets have been sold out ...")
wg.Done()
break
}
}
}
Our parameters are totally set 5 Tickets , But the program finally sold 7 Tickets . This is the security of critical resources .
3 individual goroutine Judge whether the number of tickets is greater than at a certain time 0 When , At this moment, the number of tickets is enough . But when performing the next ticket selling operation , One of them goroutine The ticket office of sold the tickets first , But the other two ticket outlets have passed the logical judgment , Also sold tickets .
How to solve the safety problem of critical resources ?
1. Most languages solve the problem of synchronous resource security through locked The way . Only one is allowed at a time goroutine To access this shared data .go In language , Can use sync Lock operation under package .
2. stay go There is a classic saying in the concurrent programming of language : Don't communicate by sharing memory , Instead, share memory by means of communication .Go Language encourages use channel Share state or change of share state in each goroutine Passed between , In this way, just like a lock, there is only one at a time goroutine Access shared state .
边栏推荐
- What can NFT metauniverse development do?
- 元宇宙系统的概念解析
- Problems caused by using ApplicationContext to render layout
- Startup and shutdown of appium service
- Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE
- Understand the execution sequence of try catch finally in one diagram
- Prototype mode
- Sword finger offer II 091 Paint the house
- The style of the mall can also change a lot. DIY can learn about it!
- The style of the mall can also change a lot. DIY can learn about it!
猜你喜欢

Continuous integration of aspnetcore & cloud flow
Client development (electron) system level API usage
After the project is pushed to the remote warehouse, Baota webhook automatically publishes it

Sword finger offer 06 Print linked list from end to end

Sword finger offer 10- I. Fibonacci sequence

Describe your understanding of the evolution process and internal structure of the method area

Geographic location data storage scheme - redis Geo
Gold three silver four, an article to solve the resume and interview

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

Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
随机推荐
Cloning and importing DOM nodes
Free books! AI across the Internet paints old photos. Here is a detailed tutorial!
SQL最常用的语句
Principle analysis of ThreadLocal source code
Why is it said that restarting can solve 90% of the problems
What is OA
Rxjs TakeUntil 操作符的学习笔记
解析数仓lazyagg查询重写优化
golang reverse a slice
Sword finger offer 03 Duplicate number in array
Super comprehensive custom deep copy function
有哪些新手程序员不知道的小技巧?
Mt60b1g16hc-48b:a micron memory particles FBGA code d8bnk[easy to understand]
合宙Air32F103CBT6開發板上手報告
In the wechat environment, H5 jumps to the specified page of the applet
Desktop development (Tauri) opens the first chapter
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE
什么是oa
Asynchronous processing of error prone points
Converting cifar10 datasets