亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Channels 讓 goroutine 相互通信

如何使用 Channels 讓 goroutine 相互通信

Go
狐的傳說 2023-08-14 14:51:10
我是 Golang 新手,正在嘗試使用 goroutine,以便它們可以在它們之間進行通信。我有一些代碼啟動一個具有操作 1 的 goroutine,我稱之為跳舞。當它完成時,它會向另一個 goroutine 發出信號,該 goroutine 執行另一個操作2,比如說睡眠。您可以將強制舞蹈參數傳遞給舞蹈 goroutine,但如果它已經處于舞蹈狀態,它將休眠。package mainimport (    "fmt"    "time")func main(){    test("notdancing", true)    time.Sleep(10*time.Second)}func dance()error{    fmt.Println("Tapping my feet")    time.Sleep(10*time.Second)    return nil}func test(status string, forceDance bool) {當   //startSleep := make(chan bool)為什么通道需要提供緩沖區長度才能使其工作?我嘗試不設置緩沖區長度,但它說如果我不傳遞 1 作為第二個參數,所有 goroutine 都會休眠。    startdance := make(chan bool, 1)    startSleep := make(chan bool, 1)    if status == "dancing" && forceDance {        select {        case startSleep <-true:            fmt.Println("Would start to sleep now")        default:            fmt.Println("Sleep Already started. No need to force")        }    }    if status != "dancing" {        fmt.Println("Startingdance")        startdance <- true    }    go func() {        <-startdance        err := dance()        if err == nil {            select {            case startSleep <- true:                fmt.Println("Starting Sleeping, dancing completed")            default:                fmt.Println("Already started Sleeping")            }        } else {            fmt.Println("Not in a mood to dance today")        }    }()    go func() {        <-startSleep        if forceDance {            fmt.Println("Force sleep because forcing to dance while already dancing")        }    }()}我非常感謝對代碼的任何更正以及使用這種方法的陷阱。
查看完整描述

1 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

在無緩沖通道的情況下(未指定大小時),它無法保存值,因為它沒有大小。因此,通過通道寫入/傳輸數據時必須有讀取器在場,否則將阻塞調用。


func main() {

? ? startDance := make(chan bool)

? ? startDance <- true

}

但是,當您在上面的代碼中指定大?。ɡ?1)時,就不會出現死鎖,因為它將有空間來保存數據。((https://robertbasic.com/blog/buffered-vs-unbuffered-channels-in-golang/)。)(https://www.golang-book.com/books/intro/10)你可以看看上述網站是為了更好地了解通道和并發性

package main


import (

? ? "fmt"

? ? "time"

)


func main() {

? ? startDance := make(chan bool)

? ? startSleep := make(chan bool)

? ? forceSleep := make(chan bool)

? ? go startDance1(startDance, forceSleep, startSleep)

? ? go performSleep(startSleep, startDance)

? ? startDance <- true

? ? fmt.Println("now dance is started ")

? ? forceSleep <- true

? ? select {}

}


func startDance1(startDance chan bool, forceSleep chan bool, startSleep chan bool) {


? ? fmt.Println("waiting to start dance")

? ? select {

? ? case <-startDance:

? ? ? ? fmt.Println("staring dance")

? ? }


? ? for {

? ? ? ? select {

? ? ? ? case <-startDance:

? ? ? ? ? ? fmt.Println("starting dance")

? ? ? ? case <-forceSleep:

? ? ? ? ? ? fmt.Println("aleardy dancing going to sleep")

? ? ? ? ? ? select {

? ? ? ? ? ? case startSleep <- true:


? ? ? ? ? ? default:

? ? ? ? ? ? }

? ? ? ? default:

? ? ? ? ? ? //this is just to show working this

? ? ? ? ? ? // i added default or else this will go into deadlock

? ? ? ? ? ? fmt.Println("dancing")

? ? ? ? ? ? time.Sleep(time.Second * 1)

? ? ? ? }

? ? }

}


func performSleep(startSleep chan bool, startDance chan bool) {

? ? select {

? ? case <-startSleep:

? ? ? ? fmt.Println("staring sleep")

? ? }

? ? fmt.Println("sleeping for 5 seconds ")

? ? time.Sleep(time.Second * 5)

? ? select {

? ? case startDance <- true:

? ? ? ? fmt.Println("started dance")

? ? default:

? ? ? ? fmt.Println("failed to start dance ")

? ? }

}

上面的代碼是對你的代碼的一個小小的改進(我試圖根據你的要求來制作它)。

查看完整回答
反對 回復 2023-08-14
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號