从不同的goroutine访问一个通道

huangapple go评论81阅读模式
英文:

Accessing a channel from a different goroutine

问题

我目前有以下代码:

package main

import (
	"fmt"
	"math/rand"
	"time"
)

var channel = make(chan []float32, 1)

func main() {
	aMap := initMap()

	for key := range aMap {
		fmt.Print("迭代次数为:", key)

		theSlice := aMap[key]
		channel <- theSlice
		go transition(channel)
		time.Sleep(2 * time.Second)

	}
}

func transition(channel chan []float32) {
	newSlice := make([]float32, 5)
	newSlice = <-channel
	fmt.Println(newSlice)

	//需要从另一个切片中检索最后一个元素,并将其放在这个新切片中
}

func initMap() map[int][]float32 {
	aMap := make(map[int][]float32)
	for i := 0; i < 2; i++ {
		aMap[i] = []float32{rand.Float32(), rand.Float32(), rand.Float32(), rand.Float32(), rand.Float32()}
	}
	return aMap
}

我们有一个包含两个切片的映射,我正在尝试的是将切片1的最后一个元素作为切片2的最后一个元素,反之亦然。

我实际上正在处理一个更大的项目,涉及细胞的分裂和分化模拟。由于这是一个模拟,所有的分裂和分化都是同时进行的。问题出在分化上,其中一个类型A的细胞会转化为类型B的细胞。每种类型的细胞都存储在不同的数据结构中,我想使用一个通道来处理每个不同的数据结构,并使用通道并发地更改数据。

希望这样说得清楚。有什么建议吗?

谢谢,
CJ

编辑:看起来我的问题不太清楚。我上面的例子中的问题是,如何同时交换这两个切片的最后一个元素?

英文:

I currently have the following code:

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;time&quot;
)

var channel = make(chan []float32, 1)

func main() {
	aMap := initMap()

	for key := range aMap {
		fmt.Print(&quot;the iteration number is: &quot;, key)

		theSlice := aMap[key]
		channel &lt;- theSlice
		go transition(channel)
		time.Sleep(2 * time.Second)

	}
}

func transition(channel chan []float32) {
	newSlice := make([]float32,5)
	newSlice = &lt;- channel
	fmt.Println(newSlice)
	
	//need to retrieve the last element of the other slice and put it on this newSlice
}

func initMap() map[int][]float32 {
	aMap := make(map[int][]float32)
	for i := 0; i &lt; 2; i++ {
		aMap[i] = []float32{rand.Float32(), rand.Float32(), rand.Float32(), rand.Float32(), rand.Float32()}
	}
	return aMap
}

We have a map with two slices and what i'm trying to do is take the last element of slice1 and make it the last element of slice2 and vice versa.

I'm actually working on a bigger project that involves simulating the division and differentiation of cells. Since its a simulation, all the divisions and differentiations are going on at the same time. The issue is with differentiation, where a cell of typeA is transformed into a cell of typeB. Each type of cell is stored in a different data structure and i figured i could use one channel for each different data structure and use the channels to change the data concurrently.

Hope this makes sense. Any suggestions?

Thanks,
CJ

EDIT: it seems what i'm asking isn't clear. My question for the example above is, how would i go about inter-changing the last elements of each of the two slices concurrently?

答案1

得分: 1

通道是双向的,用于通信,而不是共享内存。因此,也许可以改为只发送每个切片中的最后一项,将其单向发送到通道中,然后将第二个值返回另一个方向。

func main() {
  ...
  // 只发送最后一项
  theSlice := aMap[key]
  channel <- theSlice[len(theSlice)-1]
  // 等待响应
  theSlice[len(theSlice)-1] = <-channel
}

func transition(channel chan []float32) {
  newSlice := make([]float32, 5)
  channel <- newSlice[len(newSlice)-1]
  newSlice[len(newSlice)-1] = <-channel
  fmt.Println(newSlice)
}

上述代码假设通道声明已从切片更改为只是一个 float32:

var channel = make(chan float32, 1)
英文:

Channels are two way, and are about communication, not sharing memory. So perhaps instead of sending the entire slice, just sending the last item in each slice one way down the channel and then sending the second value back the other way.

  func main() {

    ...
    // send only the last item
    theSlice := aMap[key]
    channel &lt;- theSlice[len(theSlice)-1]
    // waiting for the response
    theSlice[len(theSlice)-1] = &lt;- channel
  }

  func transition(channel chan []float32) {
    newSlice := make([]float32,5)
    channel &lt;- newSlice[len(newSlice)-1]
    newSlice[len(newSlice)-1] = &lt;- channel
    fmt.Println(newSlice)
 }

The above code does assume that the channel declaration has been changed away from a slice to just a float32 :

 var channel = make(chan float32, 1)

huangapple
  • 本文由 发表于 2014年5月1日 14:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/23403302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定