glang中sync.map包详解
2021/11/30 23:36:47
本文主要是介绍glang中sync.map包详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
type Map struct { mu Mutex // read contains the portion of the map's contents that are safe for // concurrent access (with or without mu held). // // The read field itself is always safe to load, but must only be stored with // mu held. // //read 包含可安全并发访问的映射内容部分(有或没有mu持有)。 //读取字段本身始终可以安全加载,但必须仅在保留mu 的情况下存储。 //存储在 read 中的条目可以在没有mu 的情况下同时更新,但更新先前删除的条目需要将条目复制到脏映射并 //在保留mu 的情况下取消删除。 read atomic.Value // readOnly // dirty contains the portion of the map's contents that require mu to be // held. To ensure that the dirty map can be promoted to the read map quickly, // it also includes all of the non-expunged entries in the read map. // // Expunged entries are not stored in the dirty map. An expunged entry in the // clean map must be unexpunged and added to the dirty map before a new value // can be stored to it. // // If the dirty map is nil, the next write to the map will initialize it by // making a shallow copy of the clean map, omitting stale entries. dirty map[interface{}]*entry // misses counts the number of loads since the read map was last updated that // needed to lock mu to determine whether the key was present. // // Once enough misses have occurred to cover the cost of copying the dirty // map, the dirty map will be promoted to the read map (in the unamended // state) and the next store to the map will make a new dirty copy. misses int }
sync.map是线程安全的
方法:
Load() => 设置值
Store() => 获取值
LoadOrStore() => LoadOrStore 返回键的现有值(如果存在)。 否则,它存储并返回给定的值。 如果值已加载,则加载结果为 true,如果已存储则为 false
LoadAndDelete() => LoadAndDelete 删除键的值,如果有,则返回先前的值。 加载的结果报告密钥是否存在
Delete() => 删除
Range() => 循环
这篇关于glang中sync.map包详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享