当前位置:网站首页>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 .
边栏推荐
- Converting cifar10 datasets
- The database records are read through the system time under the Android system, causing the problem of incomplete Reading Records!
- Mark the same items in the Li list in red
- f_ Read function [easy to understand]
- Read the configuration, explain the principle and read the interview questions. I can only help you here...
- Sword finger offer 04 Find in 2D array
- Load local cifar10 dataset
- iVX低代码平台系列详解 -- 概述篇(一)
- 《睡眠公式》:怎么治睡不好?
- 面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
猜你喜欢
Power representation in go language

Binocular 3D perception (I): preliminary understanding of binocular

Uncover gaussdb (for redis): comprehensive comparison of CODIS

VectorDraw Developer Framework 10.1001 Crack
Super comprehensive custom deep copy function

不要再「外包」AI 模型了!最新研究发现:有些破坏机器学习模型安全的「后门」无法被检测到

TFIDF and BM25

10款超牛Vim插件,爱不释手了

The style of the mall can also change a lot. DIY can learn about it!
Client development (electron) system level API usage
随机推荐
Consumer and producer cases of inter thread synchronization (condition variable)
Analysis of the concept of metacosmic system
Create raspberry PI image file of raspberry pie
Golang regular regexp package uses -05- extend expand(), cut split() according to the rule
Converting cifar10 datasets
[Third Party framework] retrofit2 (2) - add point configuration of network access framework
What are the reasons why the game industry needs high defense servers?
普通人的2022春招总结(阿里、腾讯offer)
The style of the mall can also change a lot. DIY can learn about it!
Don't underestimate the integral mall, its role can be great!
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE
解析数仓lazyagg查询重写优化
Popular cross domain
Cloning and importing DOM nodes
Introduction to MgO 256gb NAND flash chip
Uncover gaussdb (for redis): comprehensive comparison of CODIS
分享自己平时使用的socket多客户端通信的代码技术点和软件使用
Continuously improve the overall performance of adaoracle Oracle Oracle
Sword finger offer 07 Rebuild binary tree
TFIDF and BM25