Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Concurrency 为什么我的go频道被阻塞了?(僵局)_Concurrency_Go - Fatal编程技术网

Concurrency 为什么我的go频道被阻塞了?(僵局)

Concurrency 为什么我的go频道被阻塞了?(僵局),concurrency,go,Concurrency,Go,“complex”(getC)函数似乎已被阻止。我假设通道在读取后会被破坏,因此我想知道如何在不陷入死锁的情况下与getC函数和main函数共享sC通道() 主程序包 func main(){ //简单功能和复杂功能/通道 sC:=制造(成串) 围棋(sC) cC:=制造(成串) go getC(sC、cC) //收集函数结果 s:=您不应该尝试从主函数中的sC通道获取值,因为您发送给它的唯一值是由单独的go例程中的getC函数消耗的。当尝试读取sC通道主功能块等待某些内容时,它永远不会结束。g

“complex”(
getC
)函数似乎已被阻止。我假设通道在读取后会被破坏,因此我想知道如何在不陷入死锁的情况下与
getC
函数和
main
函数共享
sC
通道()

主程序包
func main(){
//简单功能和复杂功能/通道
sC:=制造(成串)
围棋(sC)
cC:=制造(成串)
go getC(sC、cC)
//收集函数结果

s:=您不应该尝试从主函数中的
sC
通道获取值,因为您发送给它的唯一值是由单独的go例程中的
getC
函数消耗的。当尝试读取
sC
通道主功能块等待某些内容时,它永远不会结束。go例程
获取
完成后,继续e
getC
已经消耗了通道
sC
的值,并且已经完成。通道
sC
中不再有任何内容

可能的解决方案是创建另一个通道
s2C
,并向其发送从
sC
通道接收到的值

完整正确的代码如下所示:

package main

func main() {
    sC := make(chan string)
    go getS(sC)

    s2C := make(chan string)
    cC := make(chan string)
    go getC(s2C, cC)

    s := <-sC
    println(s)
    s2C <- s

    c := <-cC
    println(c)
}

func getS(sC chan string) {
    s :=  " simple completed "
    sC <- s
}

func getC(sC chan string, cC chan string) {
    s := <-sC
    c := s + " more "
    cC <- c
}
主程序包
func main(){
sC:=制造(成串)
围棋(sC)
s2C:=制造(成串)
cC:=制造(成串)
go getC(s2C,cC)

s:=我应该从下面的get.Code发送s

package main

import "time"

func main() {

    //simple function and complex function/channel
    sC := make(chan string)
    go getS(sC)

    cC := make(chan string)
    go getC(sC, cC)

    //collect the functions result

    s := <-sC
    //do something with `s`. We print but we may want to use it in a `func(s)`
    print(s)
    //after a while we do soemthing with `c`
    c := <-cC

    print(c)
}

func getS(sC chan string) {
    s := " simple completed \n"
    sC <- s
    print("sent s back so that main can read it too")
    sC <- s
}

func getC(sC chan string, cC chan string) {
    time.Sleep(1 * time.Second)
    //we do some complex stuff
    print("complex is not complicated\n")
    //Now  we need the simple value so we try wait for the s channel.
    s := <-sC


    c := s + " more "
    cC <- c //send complex value
}
主程序包
导入“时间”
func main(){
//简单功能和复杂功能/通道
sC:=制造(成串)
围棋(sC)
cC:=制造(成串)
go getC(sC、cC)
//收集函数结果

s:=我认为问题在于同步。使用睡眠可能会解决问题。当您在通道上发送值时,它应该在另一端接收,否则将显示死锁错误。 包干管

import "sync"
import "time"
import "fmt"

var wg sync.WaitGroup

func main() {
    sC := make(chan string)
    wg.Add(1)
    go getS(sC)

    cC := make(chan string)
    wg.Add(1)
    go getC(sC, cC)

    time.Sleep(1 * time.Millisecond)
    select {
    case s := <-sC:
        print(s)

    case c := <-cC:
        print(c)
    }

    wg.Wait()
}
func getS(sC chan string) {
    defer wg.Done()
    s := " simple completed "
    fmt.Println(s)
    sC <- s
}

func getC(sC chan string, cC chan string) {
    defer wg.Done()
    fmt.Println("complex is not complicated\n")
    s := <-sC
    c := s + " more "
    cC <- c //send complex value
}
导入“同步”
导入“时间”
输入“fmt”
var wg sync.WaitGroup
func main(){
sC:=制造(成串)
工作组.添加(1)
围棋(sC)
cC:=制造(成串)
工作组.添加(1)
go getC(sC、cC)
时间。睡眠(1*时间。毫秒)
挑选{

案例s:=但我需要“s”main中的值,以便我可以使用它运行另一个go例程。我是否应该打开一个重复的sC通道?@用户没有我不理解的内容。是否要将一个值发送到通道并从通道中读取两次?基本上就是这样。我需要在
main
函数中打印
s
,但
getC
还需要它来构造
c
。我不确定这个问题是否不清楚,但似乎答案很明显(过了一段时间)。
import "sync"
import "time"
import "fmt"

var wg sync.WaitGroup

func main() {
    sC := make(chan string)
    wg.Add(1)
    go getS(sC)

    cC := make(chan string)
    wg.Add(1)
    go getC(sC, cC)

    time.Sleep(1 * time.Millisecond)
    select {
    case s := <-sC:
        print(s)

    case c := <-cC:
        print(c)
    }

    wg.Wait()
}
func getS(sC chan string) {
    defer wg.Done()
    s := " simple completed "
    fmt.Println(s)
    sC <- s
}

func getC(sC chan string, cC chan string) {
    defer wg.Done()
    fmt.Println("complex is not complicated\n")
    s := <-sC
    c := s + " more "
    cC <- c //send complex value
}