最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

golang線程安全的map實現(xiàn)

 更新時間:2019年03月11日 10:11:13   作者:hackssssss  
這篇文章主要介紹了golang線程安全的map實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

網(wǎng)上找的協(xié)程安全的map都是用互斥鎖或者讀寫鎖實現(xiàn)的,這里用單個協(xié)程來實現(xiàn)下,即所有的增刪查改操作都集成到一個goroutine中,這樣肯定不會出現(xiàn)多線程并發(fā)訪問的問題。

基本思路是后臺啟動一個長期運行的goroutine,阻塞的接受自己channel中的請求req,req分為不同的請求,比如讀key,寫key等,然后在這個goroutine中進行各種操作。

例: Get方法向readSig(channel)中發(fā)送一條請求。請求是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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • go-zero創(chuàng)建RESTful API 服務(wù)的方法

    go-zero創(chuàng)建RESTful API 服務(wù)的方法

    文章介紹了如何使用go-zero框架和goctl工具快速創(chuàng)建RESTfulAPI服務(wù),通過定義.api文件并使用goctl命令,可以自動生成項目結(jié)構(gòu)、路由、請求和響應(yīng)模型以及處理邏輯,感興趣的朋友一起看看吧
    2024-11-11
  • gin框架中使用JWT的定義需求及解析

    gin框架中使用JWT的定義需求及解析

    這篇文章主要為介紹了gin框架中使用JWT的定義需求及解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • 詳解minio分布式文件存儲

    詳解minio分布式文件存儲

    MinIO 是一款基于 Go 語言的高性能、可擴展、云原生支持、操作簡單、開源的分布式對象存儲產(chǎn)品,這篇文章主要介紹了minio分布式文件存儲,需要的朋友可以參考下
    2023-10-10
  • 詳解Go中defer與return的執(zhí)行順序

    詳解Go中defer與return的執(zhí)行順序

    Go?defer中改變return的值會生效嗎,這就設(shè)計到了GO語言中defer與return哪個先執(zhí)行的問題了,下面小編就通過簡單的示例來和大家講講吧
    2023-07-07
  • GO語言類型轉(zhuǎn)換和類型斷言實例分析

    GO語言類型轉(zhuǎn)換和類型斷言實例分析

    這篇文章主要介紹了GO語言類型轉(zhuǎn)換和類型斷言,以實例形式詳細分析了類型轉(zhuǎn)換和類型斷言的概念與使用技巧,需要的朋友可以參考下
    2015-01-01
  • Go語言共享內(nèi)存讀寫實例分析

    Go語言共享內(nèi)存讀寫實例分析

    這篇文章主要介紹了Go語言共享內(nèi)存讀寫方法,實例分析了共享內(nèi)存的原理與讀寫技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-02-02
  • golang如何通過viper讀取config.yaml文件

    golang如何通過viper讀取config.yaml文件

    這篇文章主要介紹了golang通過viper讀取config.yaml文件,圍繞golang讀取config.yaml文件的相關(guān)資料展開詳細內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • GO Cobra Termui庫開發(fā)終端命令行小工具輕松上手

    GO Cobra Termui庫開發(fā)終端命令行小工具輕松上手

    這篇文章主要為大家介紹了GO語言開發(fā)終端命令行小工具,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Golang開發(fā)庫的集合及作用說明

    Golang開發(fā)庫的集合及作用說明

    這篇文章主要為大家介紹了Golang開發(fā)golang庫的集合及簡單的作用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-11-11
  • 優(yōu)雅使用GoFrame共享變量Context示例詳解

    優(yōu)雅使用GoFrame共享變量Context示例詳解

    這篇文章主要為大家介紹了優(yōu)雅使用GoFrame共享變量Context示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06

最新評論

海南省| 阿鲁科尔沁旗| 龙口市| 宁阳县| 平定县| 年辖:市辖区| 富锦市| 东阿县| 瑞安市| 宁远县| 华容县| 简阳市| 江源县| 怀远县| 汾阳市| 外汇| 吴桥县| 红安县| 余庆县| 南召县| 屏东市| 洞头县| 永寿县| 鲁山县| 屏东县| 海盐县| 理塘县| 万山特区| 定安县| 自贡市| 巴中市| 丹凤县| 星子县| 新密市| 绥阳县| 左权县| 武威市| 呼和浩特市| 余姚市| 朝阳市| 任丘市|