2015-12-03 56 views
0

我有一個循環,顯然是造成數據爭其接近這個函數的底部,我將它標誌着:想不通,爲什麼這個循環是一個數據的比賽

func (p *PartialParty) SendReadyCheck(party PartialParty) { 
    msg, err := json.Marshal(&ReadyCheckMsg{"ReadyCheck", ""}) 
    if err != nil { 
     log.Println(err) 
    } 

    for _, member := range party.Members { 
     member.Conn.send <- msg 
    } 

    counter := 0 
    loopBreaker := true 
    for { 
     select { 
     case <-p.Accept: 
      counter++ 
      resp, err := json.Marshal(&ReadyCheckMsg{"ReadyAccepted", ""}) 
      if err != nil { 
       log.Println(err) 
      } 
      for _, member := range party.Members { 
       member.Conn.send <- resp 
      } 
      if counter == 2 { 
       // Create a new party with all members 
       partyid := PartyID(feeds.NewUUID().String()) 
       db := common.Db() 
       newParty := &Party{ 
        Active: true, 
        Members: p.Members, 
        Broadcast: make(chan []byte), 
        PartyID: partyid, 
       } 

       // Insert the new party into the database 
       _, err := db.Exec("INSERT INTO party SET party_id = ?, active = ?", partyid.String(), true) 
       if err != nil { 
        log.Println(err) 
       } 

       // Go through the members and update the database 
       var wg sync.WaitGroup 

       for _, member := range party.Members { 
        wg.Add(1) 
        m := member 
        go func() { 
         _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, m.SteamID) 
         if err != nil { 
          log.Println(err) 
         } 
         _, err = db.Exec("INSERT INTO party_members SET belongs_to =?, active = ?, steamid = ?", partyid.String(), true, m.SteamID) 
         if err != nil { 
          log.Println(err) 
         } 
         wg.Done() 
        }() 
       } 

       // Wait for all the database stuff to finish 
       wg.Wait() 
       PHub.AddNewParty(newParty) 
       loopBreaker = false 
       break 
      } 
     case conn := <-p.Decline: 
      if conn.Ready { 
       break 
      } 
      conn.Ready = false 
      conn.InQueue = false 
      conn.CurrentParty = "" 
      resp, err := json.Marshal(&ReadyCheckMsg{"ReadyCheckDeclined", ""}) 
      if err != nil { 
       log.Println(err) 
      } 
      p.Accepting = true 
      identifier := conn.Identifier 
      if _, ok := party.Members[identifier]; ok { 
       delete(p.Members, identifier) 
      } 
      for _, m := range party.Members { 
       member := m 
       member.Conn.send <- resp 
      } 
      log.Println("Here") 
      loopBreaker = false 
      break 
     case <-time.After(30 * time.Second): 
      if counter == 2 { 
       return 
      } 
      p.Accepting = true 
      failedMsg, err := json.Marshal(&ReadyCheckMsg{"FailedToReady", ""}) 
      if err != nil { 
       log.Println(err) 
      } 
      somebodyDeclinedMsg, err := json.Marshal(&ReadyCheckMsg{"ReadyCheckDeclined", ""}) 
      if err != nil { 
       log.Println(err) 
      } 


    >>>>  for _, member := range party.Members { ***<<<< This Line*** 
       m := member 
       if !m.Conn.Ready { 
        m.Conn.Ready = false 
        m.Conn.InQueue = false 
        m.Conn.CurrentParty = "" 
        m.Conn.send <- failedMsg 
       } else { 
        m.Conn.Ready = false 
        m.Conn.send <- somebodyDeclinedMsg 
       } 
      } 
      loopBreaker = false 
      break 
     } 
     if !loopBreaker { 
      break 
     } 
    } 
} 

這顯然是與此相沖突:

// AddNewMember will add a new user to the party 
func (p *PartyHub) AddNewMember(member *Member, partyid PartyID) { 
    p.Lock() 
    defer p.Unlock() 
>>> p.PartialPartys[partyid].Members[member.Conn.Identifier] = member 
} 

type PartialParty struct { 
    Accepting bool 
    Members map[Identifier]*Member 
    Accept chan *Connection 
    Decline chan *Connection 
    PartyID PartyID 
    sync.Mutex 
} 

現在這是不可能AddNewMember如果夠程SendReadyCheck運行``因爲它是通過檢查,如果夠程運行if語句保護的,所以我不知道他們爲什麼他們說他們在互相競爭。任何幫助清理這件事都很好。我已經嘗試設置循環內部的變量,試圖遠離它,但它似乎並沒有引起它

回答

0

現在這是不可能AddNewMember如果夠程SendReadyCheck運行``因爲它是受if語句保護,該語句檢查goroutine是否正在運行

您並未真正顯示該代碼的一部分,但大概並非不可能。如果SendReadyCheck在之後開始運行if if test但在AddNewMember之前做了修改?