Map with concurrent access

前端 未结 6 1357

When you use a map in a program with concurrent access, is there any need to use a mutex in functions to read values?

6条回答
  •  猫巷女王i
    2020-11-29 01:52

    Why no made use of Go concurrency model instead, there is a simple example...

    type DataManager struct {
        /** This contain connection to know dataStore **/
        m_dataStores map[string]DataStore
    
        /** That channel is use to access the dataStores map **/
        m_dataStoreChan chan map[string]interface{}
    }
    
    func newDataManager() *DataManager {
        dataManager := new(DataManager)
        dataManager.m_dataStores = make(map[string]DataStore)
        dataManager.m_dataStoreChan = make(chan map[string]interface{}, 0)
        // Concurrency...
        go func() {
            for {
                select {
                case op := <-dataManager.m_dataStoreChan:
                    if op["op"] == "getDataStore" {
                        storeId := op["storeId"].(string)
                        op["store"].(chan DataStore) <- dataManager.m_dataStores[storeId]
                    } else if op["op"] == "getDataStores" {
                        stores := make([]DataStore, 0)
                        for _, store := range dataManager.m_dataStores {
                            stores = append(stores, store)
                        }
                        op["stores"].(chan []DataStore) <- stores
                    } else if op["op"] == "setDataStore" {
                        store := op["store"].(DataStore)
                        dataManager.m_dataStores[store.GetId()] = store
                    } else if op["op"] == "removeDataStore" {
                        storeId := op["storeId"].(string)
                        delete(dataManager.m_dataStores, storeId)
                    }
                }
            }
        }()
    
        return dataManager
    }
    
    /**
     * Access Map functions...
     */
    func (this *DataManager) getDataStore(id string) DataStore {
        arguments := make(map[string]interface{})
        arguments["op"] = "getDataStore"
        arguments["storeId"] = id
        result := make(chan DataStore)
        arguments["store"] = result
        this.m_dataStoreChan <- arguments
        return <-result
    }
    
    func (this *DataManager) getDataStores() []DataStore {
        arguments := make(map[string]interface{})
        arguments["op"] = "getDataStores"
        result := make(chan []DataStore)
        arguments["stores"] = result
        this.m_dataStoreChan <- arguments
        return <-result
    }
    
    func (this *DataManager) setDataStore(store DataStore) {
        arguments := make(map[string]interface{})
        arguments["op"] = "setDataStore"
        arguments["store"] = store
        this.m_dataStoreChan <- arguments
    }
    
    func (this *DataManager) removeDataStore(id string) {
        arguments := make(map[string]interface{})
        arguments["storeId"] = id
        arguments["op"] = "removeDataStore"
        this.m_dataStoreChan <- arguments
    }
    

提交回复
热议问题