当前位置:网站首页>Go language learning notes - mutex start go language from scratch
Go language learning notes - mutex start go language from scratch
2022-07-27 21:59:00 【web18224617243】
Learning notes , Where is where .
Only one coroutine can access the resource at the same time , Other threads can only wait , Mutual exclusivity . Mutexes are often used in work projects , But pay attention to deadlock , That is, you must pay attention to whether the process of releasing the lock can be executed .
The sample code is as follows
package main
import (
"fmt"
"sync"
"time"
)
var (
count int
myLock sync.Mutex
wg1 sync.WaitGroup
)
func GetCount() int {
myLock.Lock()
fmt.Println("GetCount lock ")
defer myLock.Unlock()
defer fmt.Println("GetCount unlock ")
return count
}
func IncCount() {
myLock.Lock()
fmt.Println("IncCount lock ")
defer myLock.Unlock()
defer fmt.Println("IncCount unlock ")
count += 1
}
func main() {
wg1.Add(2)
go func() {
defer wg1.Done()
for i := 0; i < 11; i++ {
fmt.Println("count -> ", GetCount())
time.Sleep(1 * time.Second)
}
}()
go func() {
defer wg1.Done()
for i := 0; i < 10; i++ {
IncCount()
fmt.Println("count -> add 1")
time.Sleep(1 * time.Second)
}
}()
wg边栏推荐
- Technical practice behind bloom model: how to refine 176billion parameter model?
- Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
- Station B collapsed. If we were the developer responsible for the repair that night
- Idea connects to MySQL database and performs SQL query operations
- Pytoch distributed training
- 一口气学完 Redis 集群方案
- Mobilevit learning notes
- LInkedList底层源码
- Box model and element positioning
- 声扬科技正式上线闻声远程声纹健康回访服务系统!
猜你喜欢
![[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/c4/71a9933a3a1fdd14f84a41b640f5b5.jpg)
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years

Exception -exception

It seems to be a bug of thread pool, but I think the source code design is unreasonable.

An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!

单核CPU, 1G内存,也能做JVM调优吗?

Form of objects in memory & memory allocation mechanism

深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))

Can JVM tuning be done with single core CPU and 1G memory?

day 1 - day 4
随机推荐
[day_4-review, basic concepts of objects and arrays - 1]
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
More than 100 lines should be split into functions
@Can component be used on the same class as @bean?
软件测试面试题:设计系统测试计划需要参考的项目文档?
LM NAV: robot navigation method based on large models of language, vision and behavior
University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
Encapsulate an array into a class
Cocoapods reload
Finish learning redis cluster solution at one go
Station B collapsed. If we were the developer responsible for the repair that night
软件测试面试题:系统测试的策略有多少种?
聊聊 MySQL 事务二阶段提交
一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!
An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
Software testing interview question: how many strategies are there for system testing?
Log4j 漏洞仍普遍存在,并持续造成影响
How to deal with high concurrency deadlock?
Box model and element positioning
CocoaPods 重装