当前位置:网站首页>Golang Chan implements mutual exclusion
Golang Chan implements mutual exclusion
2022-07-27 18:10:00 【gitxuzan_】
// Use chan Implement mutually exclusive lock
type Mutex struct {
ch chan struct{
}
}
// Using locks requires initialization
func NewMutex() *Mutex {
mu := &Mutex{
make(chan struct{
}, 1)}
mu.ch <- struct{
}{
}
return mu
}
// Request lock , Until you get
func (m *Mutex) Lock() {
<-m.ch
}
// Unlock
func (m *Mutex) Unlock() {
select {
case m.ch <- struct{
}{
}:
default:
panic("unlock of unlocked mutex")
}
}
// Attempt to acquire lock
func (m *Mutex) TryLock() bool {
select {
case <-m.ch:
return true
default:
}
return false
}
// Add a timeout setting
func (m *Mutex) LockTimeout(timeout time.Duration) bool {
timer := time.NewTimer(timeout)
select {
case <-m.ch:
timer.Stop()
return true
case <-timer.C:
}
return false
}
// Whether the lock has been held
func (m *Mutex) IsLocked() bool {
return len(m.ch) == 0
}
func main() {
m := NewMutex()
ok := m.TryLock()
fmt.Printf("locked v %v\n", ok)
ok = m.TryLock()
fmt.Printf("locked %v\n", ok)
}
边栏推荐
- WPF做登陆界面
- Convolutional neural network -- Translation of yolov2 (yolo9000) papers
- 防止sql注入
- TCP connection status identification (syn, fin, ACK, PSH, RST, urg)
- Error launching IDEA
- Bubble sorting in JS
- wallys/DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power.
- 7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制
- 机器学习之评价指标(一)——回归评价指标
- Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps
猜你喜欢

机器学习之评价指标(一)——回归评价指标

Salesforce certified sharing and visibility Designer (su20) certification examination summary

Knowledge dry goods: basic storage service novice Experience Camp
What are the safety risks of small games?
知物由学 | 小游戏的安全风险在哪里?

Bug records using distributed framework WCF

Convolutional neural network -- Translation of yolov2 (yolo9000) papers

Code compliance: five reasons why developers use helix QAC

7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制

Notes on standardized management of "ancestral warehouse" of meituan meal
随机推荐
Machine learning: IOU of concept understanding
面试常见问题一二
Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks
The latest advanced interview questions for big factories are necessary
[MCU] 2.3 CPU of AT89S52
golang 并发缓存击穿或合并请求
Activate genuine win7 ultimate system with slmgr command
2022 high altitude installation, maintenance and removal of test question simulation test platform operation
Multi thread import data and generate error files for redis storage
Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination
[introduction to database system (Wang Shan)] Chapter 5 - database integrity
Resolve merge fields in salesforce
In the first week of June, risk control of e-shield business paid attention to 15 institutions such as New Oriental XRS, which were fined
The global cloud market is growing rapidly, and data security has entered a strong regulatory era of rule of law
细数国产接口协作平台的六把武器!
知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道
Configuration and basic use of vim
机器学习——概念理解之IoU
防止sql注入
What's the use of games| Game application value research case collection