当前位置: 首页 > news >正文

go同步锁 sync mutex

goroutine

http://127.0.0.1:3999/concurrency/11

go tour 到此 就结束了.

继续 学习 可以 从 以下网站

文档

https://golang.org/doc/

https://golang.org/doc/code

https://golang.org/doc/codewalk/functions/

博客

https://go.dev/blog/

wiki 服务器教程

服务器 教程 入口
https://golang.org/doc/articles/wiki/

https://github.com/gin-gonic/gin

官网

https://golang.org/

sync.mutex Lock


package mainimport ("fmt""sync""time"
)// SafeCounter is safe to use concurrently.
type SafeCounter struct {mu sync.Mutexv  map[string]int
}// Inc increments the counter for the given key.
func (c *SafeCounter) Inc(key string) {c.mu.Lock()// Lock so only one goroutine at a time can access the map c.v.c.v[key]++c.mu.Unlock()
}// Value returns the current value of the counter for the given key.
func (c *SafeCounter) Value(key string) int {c.mu.Lock()// Lock so only one goroutine at a time can access the map c.v.defer c.mu.Unlock()return c.v[key]
}func main() {c := SafeCounter{v: make(map[string]int)}for i := 0; i < 1000; i++ {go c.Inc("somekey")}time.Sleep(time.Second)fmt.Println(c.Value("somekey"))
}
http://www.lryc.cn/news/237802.html

相关文章:

  • 使用项目自动生成的dokcerfile第一次构建时把加载aps5.0失败无法找到加载的文件
  • ACREL DC energy meter Application in Indonesia
  • 报错!Jupyter notebook 500 : Internal Server Error
  • 2023.11.21使用<button>元素来触发form表单和数据提交
  • leetcode:504. 七进制数
  • centos安装指定版本docker
  • PPT幻灯片里的图片,批量提取
  • 《Fine-Grained Image Analysis with Deep Learning: A Survey》阅读笔记
  • 【网络安全】伪装IP网络攻击的识别方法
  • redis非关系型数据库
  • LeetCode 0053. 最大子数组和:DP 或 递归(线段树入门题?)
  • 二十三种设计模式全面解析-解密职责链模式:请求处理的设计艺术
  • 【linux】安装telnet
  • 深入探索 PaddlePaddle 中的计算图
  • 西南科技大学814考研一
  • 【网络编程】简述TCP通信程序,三次握手,四次挥手
  • 【ARM Trace32(劳特巴赫) 使用介绍 5 -- Trace32 ELF 文件加载介绍】
  • Linux(4):Linux文件与目录管理
  • Altium Designer学习笔记2
  • Atlassian发布最新补贴政策,Jira/Confluence迁移上云最低可至零成本
  • 基于FPGA的五子棋(论文+源码)
  • QT5 MSVC2017 64bit配置OpenCV4.5无需编译与示范程序
  • windows如何查看自己的ip地址
  • Camera2的使用【详细】
  • Playcanvas后处理-辉光bloom
  • GCC 学习
  • 2023数维杯数学建模C题完整版本
  • 快速解密PPT幻灯片密码,让PPT重见天日
  • 十六、RabbitMQ快速入门
  • C#WPF用户控件及自定义控件实例