当前位置:网站首页>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)
}
边栏推荐
- Convolutional neural network -- SSD thesis translation
- WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl
- 知物由学 | 再造巴别塔,我们如何进行NLP跨语言知识迁移?
- 6月第1周易盾业务风控关注 | 新东方学而思等15家机构被顶格罚款
- Compilation and testing of raspberry pie driver code
- [Southwest University] information sharing of postgraduate entrance examination and re examination
- The global cloud market is growing rapidly, and data security has entered a strong regulatory era of rule of law
- Salesforce certified sharing and visibility Designer (su20) certification examination summary
- 防止sql注入
- Convolutional neural network -- Translation of yolov1 thesis
猜你喜欢

Personal understanding of convolution calculation process of convolution neural network

Oracle 11g database installation tutorial

知物由学 | 易盾自研文本实时聚类技术,一网打尽社交网络中的同类有害内容

Soul 1: why is es more suitable for complex condition search than MySQL?

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

Knowing things by learning | app slimming down, the way of safety reinforcement under the new generation AAB framework

知物由学 | 从0到1搭建实时反外挂机制,多维度补充手游攻防力

In the fourth week of July, Yidun business risk control focused on the supreme law to regulate app's forcible request for personal information

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

Profiles vs Permission Sets
随机推荐
Error launching IDEA
Class not found: “com.parkManagement.dao.DaoTest 测试找不到测试类
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
How to develop an online Excel spreadsheet system (Part 1)
多线程实现循环
Compilation and testing of raspberry pie driver code
面试好难啊!蚂蚁金服的六轮面试我是强撑过来!差点OUT(面试复盘)
工信部再治数据安全,网易易盾“隐私合规”守住企业经营底线
Evaluation index of machine learning (I) -- regression evaluation index
Kubernetes 1.24 high availability cluster binary deployment
知物由学 | 易盾自研文本实时聚类技术,一网打尽社交网络中的同类有害内容
知物由学 | 易盾移动端同构实践,几步改善官网交互体验
How difficult the interview is! I was forced to survive the six rounds of interview of ant financial! Almost out (interview resumption)
Operation of simulated examination platform for 2022 low voltage electrician examination questions
How to solve the error of ora-00955 when Oracle modifies the primary key
Prevent SQL injection
X-sheet development tutorial: initialization configuration custom layout
Interview FAQs 12
机器学习——概念理解之IoU
卷积神经网络——从R-CNN,Fast R-CNN到Faster R-CNN,Mask R-CNN