Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to interpret Go language Lock

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to interpret the Go language lock, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Var l sync.Mutexvar a stringfunc f () {a = "hello, world" l.Unlock ()} func main () {l.Lock () go f () l.Lock () print (a)

}

Two data types about locks, sync.Mutex and sync.RWMutex, are implemented in the sync package. [mutex mutex is exclusive, you can only lock once, unlock once, and then continue lock otherwise blocking. Read-write mutex reader-writer mutex is a lock shared by all reader or monopolized by a writer. If one reader lock has a lock, other reader can also lock but writer cannot lock. ]

For the variable mutex of type sync.Mutex or sync.RWMutex, assume n < m, and the nth call to mutex.Unlock () occurs before the m call of mutex.Lock () returns. For a mutex, once lock, the second lock will block, and only unlock can continue lock. That's what it means. But what happens to unlock, a mutex without lock? Error!]

In fact, the key point is just one thing.

After each lock is to wait for the return value of the unclock, so how to ensure that the previous value of the unlock operation is returned? go defines that each lock must occur after the last unlock. So the program is interpreted as follows:

1. Call lock once

2. Gorou performs the write operation in the face of an assignment, where the unlock operation and the write operation are in the same "thread", and the unlock is after writing.

3. Call l.lock () to make sure that you have to wait until unlock is finished at this point. That is, lock occurs after UNclock and even after assignment.

4. The second lock occurs before print, so you know.

On how to interpret the Go language lock to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report