C/C++教程

glang中sync.map包详解

本文主要是介绍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包详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!