dsghpgmay31938863
2016-10-23 18:44
浏览 84
已采纳

什么是golang中的sync(sync.RWMutex)

I am looking gorilla context to source code. I don't understand what it does sync,mutex.Lock, mutex.Unlock exactly. it still is running when I removed mutex.Lock and Mutex.Unlock from source codes.

import (
    "net/http"
    "sync"
    "time"
)

var (
    mutex sync.RWMutex
    data  = make(map[*http.Request]map[interface{}]interface{})
    datat = make(map[*http.Request]int64)
)

// Set stores a value for a given key in a given request.
func Set(r *http.Request, key, val interface{}) {
    mutex.Lock()
    if data[r] == nil {
        data[r] = make(map[interface{}]interface{})
        datat[r] = time.Now().Unix()
    }
    data[r][key] = val
    mutex.Unlock()
}

图片转代码服务由CSDN问答提供 功能建议

我正在寻找大猩猩上下文作为源代码。 我不明白它的作用是sync,mutex.Lock,mutex.Unlock。 当我从源代码中删除了互斥锁和互斥锁时,它仍然在运行。 </ p>

  import(
“ net / http” 
“ sync” 
“ time” 
)
 
var(
互斥锁同步。RWMutex
数据 = make(map [* http.Request] map [interface {}] interface {})
 datat = make(map [* http.Request] int64)
)
 
 // Set为a存储值 
func Set(r * http.Request,key,val interface {}){
 Mutex.Lock()
 if data [r] == nil {
 data [r] =  make(map [interface {}] interface {})
 datat [r] = time.Now()。Unix()
} 
 data [r] [key] = val 
 Mutex.Unlock()\  n} 
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题