douwenpin0428 2014-02-27 17:50
浏览 37
已采纳

使用for循环创建的通道开始

I was wondering if channels created in a for loop can be interchangeably used by subroutines running concurrently from that for loop?

Pseudo-code bellow:

for i := range Map {
      channel := make(chan my_type, buff_size)

      go subroutine(Map[i], channel)
}

func subroutine(name valueType, channel channelType) {
    //stuff here
}

Is there a way were subroutine(Map[0]) for example, could access another channel created during another iteration of the forloop ie channel of subroutine(Map[1])?

CONTEXT: I'm currently working on a project where I have to simulate different populations of cells. Each cell has the ability to divide/differentiate/etc. To replicate the real system, the different populations have to run concurrently with one another. The issue is that I have to insert/remove a cell of a specific population type while working on a different population, this is where the channels come into play. I was thinking of running the populations concurrently each of them having an associated channel. And this is why i'm asking if we can use channels created in different for loop iterations.

I'm hoping people understand what i'm asking.

Thanks!

EDIT: I've decided to add some of my code to support my CONTEXT description with comments explaining the different elements. I've included channels but I have no idea if it works. I'm hoping it helps with understanding what I'm trying to do :

func main() {
  //where envMap is a map[int]Population and Population is a struct
  envMap := initialiseEnvironment(envSetupInfo)

  //general simulation loop
  for i := 0; i < 10; i++ {
     //loop to go through all envMap elements in parallel 
     for eachKey := range envMap {
         channel := make(chan type)
         go simulateEnv(envMap[eachKey])
     }
  }
}

func simulateEnv(cellPopulation Population, channel chan) {
  //each Population has a map[int]Cell where Cell is a struct with cell properties
  cellMap := cellPopulation.cellMap

  for eachCell := range cellMap {
      go divisionTransition(eachCell, channel)
  }
}
  • 写回答

1条回答 默认 最新

  • dongyoucha0645 2014-02-27 19:18
    关注

    Assuming each element of the map is a struct, you could make a field to hold references to other channels. For example, if you wanted each element of your map to have a reference to the previous channel, you could do something like this:

    // assumes that Map[whatever] is a struct with a "lastChan" field
    var lastRef = chan my_type
    
    
    for i := range Map {
          channel := make(chan my_type, buff_size)
          if(lastRef != nil){
              Map[i].lastChan = lastRef
          }
    
          go subroutine(Map[i], channel)
          lastRef = channel
    }
    
    func subroutine(name valueType, channel channelType) {
        //stuff here can access the previous channel with name.lastChan
    }
    

    Depending on what you want to do and which channels you need access to, you may wish to play around with the looping or even do mutliple loops.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致