golang线程安全的map实现
作者:hackssssss 发布时间:2024-04-28 09:10:59
标签:golang,线程安全,map
网上找的协程安全的map都是用互斥锁或者读写锁实现的,这里用单个协程来实现下,即所有的增删查改操作都集成到一个goroutine中,这样肯定不会出现多线程并发访问的问题。
基本思路是后台启动一个长期运行的goroutine,阻塞的接受自己channel中的请求req,req分为不同的请求,比如读key,写key等,然后在这个goroutine中进行各种操作。
例: Get方法向readSig(channel)中发送一条请求。请求是readReq的指针,当run方法接收到信号时,读取底层map,将值写入readReq的value中(value是个channel),Get方法阻塞的接收value,接收到就返回value。
ps:花了两个多小时写完,只是简单的做了测试,没有深入测试,另外性能也没有测过,以后有空会深入测试一下正确性以及相比加锁的写法其性能如何。
package util
type smap struct {
m map[interface{}]interface{}
readSig chan *readReq
writeSig chan *writeReq
lenSig chan *lenReq
terminateSig chan bool
delSig chan *delReq
scanSig chan *scanReq
}
type readReq struct {
key interface{}
value interface{}
ok chan bool
}
type writeReq struct {
key interface{}
value interface{}
ok chan bool
}
type lenReq struct {
len chan int
}
type delReq struct {
key interface{}
ok chan bool
}
type scanReq struct {
do func(interface{}, interface{})
doWithBreak func(interface{}, interface{}) bool
brea int
done chan bool
}
// NewSmap returns an instance of the pointer of safemap
func NewSmap() *smap {
var mp smap
mp.m = make(map[interface{}]interface{})
mp.readSig = make(chan *readReq)
mp.writeSig = make(chan *writeReq)
mp.lenSig = make(chan *lenReq)
mp.delSig = make(chan *delReq)
mp.scanSig = make(chan *scanReq)
go mp.run()
return &mp
}
//background function to operate map in one goroutine
//this can ensure that the map is Concurrent security.
func (s *smap) run() {
for {
select {
case read := <-s.readSig:
if value, ok := s.m[read.key]; ok {
read.value = value
read.ok <- true
} else {
read.ok <- false
}
case write := <-s.writeSig:
s.m[write.key] = write.value
write.ok <- true
case l := <-s.lenSig:
l.len <- len(s.m)
case sc := <-s.scanSig:
if sc.brea == 0 {
for k, v := range s.m {
sc.do(k, v)
}
} else {
for k, v := range s.m {
ret := sc.doWithBreak(k, v)
if ret {
break
}
}
}
sc.done <- true
case d := <-s.delSig:
delete(s.m, d.key)
d.ok <- true
case <-s.terminateSig:
return
}
}
}
//Get returns the value of key which provided.
//if the key not found in map, ok will be false.
func (s *smap) Get(key interface{}) (interface{}, bool) {
req := &readReq{
key: key,
ok: make(chan bool),
}
s.readSig <- req
ok := <-req.ok
return req.value, ok
}
//Set set the key and value to map
//ok returns true indicates that key and value is successfully added to map
func (s *smap) Set(key interface{}, value interface{}) bool {
req := &writeReq{
key: key,
value: value,
ok: make(chan bool),
}
s.writeSig <- req
return <-req.ok //TODO 暂时先是同步的,异步的可能存在使用方面的问题。
}
//Clear clears all the key and value in map.
func (s *smap) Clear() {
s.m = make(map[interface{}]interface{})
}
//Size returns the size of map.
func (s *smap) Size() int {
req := &lenReq{
len: make(chan int),
}
s.lenSig <- req
return <-req.len
}
//terminate s.Run function. this function is usually called for debug.
//after this do NOT use smap again, because it can make your program block.
func (s *smap) TerminateBackGoroutine() {
s.terminateSig <- true
}
//Del delete the key in map
func (s *smap) Del(key interface{}) bool {
req := &delReq{
key: key,
ok: make(chan bool),
}
s.delSig <- req
return <-req.ok
}
//scan the map. do is a function which operate all of the key and value in map
func (s *smap) EachItem(do func(interface{}, interface{})) {
req := &scanReq{
do: do,
brea: 0,
done: make(chan bool),
}
s.scanSig <- req
<-req.done
}
//scan the map util function 'do' returns true. do is a function which operate all of the key and value in map
func (s *smap) EachItemBreak(do func(interface{}, interface{}) bool, condition bool) {
req := &scanReq{
doWithBreak: do,
brea: 1,
done: make(chan bool),
}
s.scanSig <- req
<-req.done
}
//Exists checks whether the key which provided is exists in map
func (s *smap) Exists(key interface{}) bool {
if _,found := s.Get(key); found {
return true
}
return false
}
github地址:https://github.com/hackssssss/safemap
来源:https://blog.csdn.net/liyunlong41/article/details/84259488


猜你喜欢
- 一开始没看懂stddev是什么参数,找了一下,在tensorflow/python/ops里有random_ops,其中是这么写的:def
- 一、Python 文件读写概述Python 在文件读写操作中,会使用「内置函数」和「Pandas 库」两种方式。先来看内置函数,包括 ope
- 在日常的生活和工作中,我们经常会遇到一些大小问题,其中有很多的问题,都是可以使用一些简单的Python代码就能解决。比如不久前的复旦大佬,用
- 本文实例讲述了Python数据类型之List列表。分享给大家供大家参考,具体如下:list列表1.概述:通过之前的学习,我们知道变量可以存储
- 两个例子package main import ( "fmt" "time")func Proces
- 本文较为深入的探究了php中in_array函数用法。分享给大家供大家参考。具体如下:今天突然想到php中的in_array函数有个其怪的用
- 在asp中调用sql server的存储过程可以加快程序运行速度,本文介绍了asp使用存储过程的方法。1.调用存储过程的一般方法 先假设在s
- 函数可以参考:<% '注册论坛用户,参数说明 'username 用户登录名称 
- 主要记录一下:图片验证码1.获取登录界面的图片2.获取验证码位置3.在登录页面截取验证码保存4.调用百度api识别(目前准确率较高的识别图片
- pip是常用的python包管理工具,用python的同学,都离不开pip ~~第一种 在系统自带的python2.7的路径下pip是pyt
- Windows下的安装:下载地址:https://pypi.python.org/pypi/pyquery/#downloads下载后安装:
- 我就废话不多说了,大家还是直接看代码吧~#! /usr/bin/env python# -*- coding:utf-8 -*-import
- 例子1:<input type="text" value="0" onkeyup="
- 文件下载(遇到的坑)1.要区分下载的内容是什么0.1 图片下载wx.saveImageToPhotosAlbum(Object
- 概述:本文将通过组织自己的训练数据,使用Pytorch深度学习框架来训练自己的模型,最终实现自己的图像分类!本篇文章以识别阳台为例子,进行讲
- 如何提高ASP的效率?通过修改注册表来提高asp的执行效率 改的第一个地方:HKEY_LOCAL_MAC
- QSplitter使用户可以通过拖动子面板的边界控制子面板的大小。在我们的例子中,我们使用了两个QSplitter 对三个QFrame 控件
- 获取nc数据的相关信息from netCDF4 import Datasetimport numpy as npimport pandas
- 前言在MySQL中,并不是你建立了索引,并且你在SQL中使用到了该列,MySQL就肯定会使用到那些索引的,有一些情况很可能在你不知不觉中,你
- 我就废话不多说啦,还是直接看代码吧!import osimport sysimport djangosys.path.append(r