第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在不等待另一個(gè) goroutine 中設(shè)置的情況下讀取頻道?

如何在不等待另一個(gè) goroutine 中設(shè)置的情況下讀取頻道?

Go
一只萌萌小番薯 2022-04-26 15:03:04
我在 goroutine 中使用通道時(shí)遇到問(wèn)題。var test = make(chan string)func main() {    go initChan()    for i := 0; i < 2; i++ {        go readChan()    }    var input string    fmt.Scanln(&input)}func initChan() {    for i := 0; i < 100; i++ {        test <- "Iteration num: " + strconv.Itoa(i)        time.Sleep(time.Second * 5)    }}func readChan() {    for {        message := <- test        log.Println(message)    }}輸出:2019/12/24 08:21:17 Iteration num: 02019/12/24 08:21:22 Iteration num: 12019/12/24 08:21:27 Iteration num: 22019/12/24 08:21:32 Iteration num: 32019/12/24 08:21:37 Iteration num: 42019/12/24 08:21:42 Iteration num: 5................................我需要線程讀取而不等待測(cè)試變量的更新?,F(xiàn)在每個(gè) readChan() 都在等待 initChan() 更新測(cè)試變量。是否可以使 readChan() 線程一次工作而無(wú)需等待每個(gè)線程的 initChan() ?
查看完整描述

2 回答

?
冉冉說(shuō)

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

創(chuàng)建了一個(gè)惡魔,它將所有消息從測(cè)試通道推送到所有其他監(jiān)聽(tīng)程序。


var test = make(chan string)


var mapChan = make(map[int]chan string)

var count = 3


func main() {

    go initChan()

    go deamon()

    for i := 0; i < count; i++ {

        mapChan[i] = make(chan string)

        go readChan(i)

    }


    var input string

    fmt.Scanln(&input)

}


func deamon() {

    for {

        message := <-test

        for i := 0; i < count; i++ {

            mapChan[i] <- message

        }

    }

}


func initChan() {

    for i := 0; i < 100; i++ {

        test <- "Iteration num: " + strconv.Itoa(i)

        time.Sleep(time.Second * 1)

    }

}


func readChan(i int) {

    for {

        select {


        case message := <-mapChan[i]:

            log.Println(message)

        default:

            // Do for not when written on channel

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-04-26
?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果我正確理解您的問(wèn)題,此解決方案可能會(huì)有所幫助。我使用了一個(gè)大小為 1 的緩沖通道,因此作為發(fā)送方的 goroutine 永遠(yuǎn)不會(huì)被阻塞,這是在無(wú)緩沖通道的情況下。您可以閱讀有關(guān)頻道的更多信息:頻道的行為


package main


import (

    "log"

    "strconv"

    "sync"

    "time"

)


// Buffered channel with size 1 guarantees delayed delivery of data

// As soon as the goroutine sends to the channel, the reciever goroutine dequeus it

// Then the reciver goroutines does the work, but the sender goroutine isn't blocked

// As the size is again 0 after the reciever recieved it but might haven't processed it yet

var test = make(chan string, 1)


func main() {

    var wg sync.WaitGroup

    wg.Add(2)

    // Waits for other goroutines to complete before the main goroutine returns

    defer wg.Wait()

    go initChan(&wg)

    go readChan(&wg)

}


func initChan(wg *sync.WaitGroup) {

    defer wg.Done()

    for i := 0; i < 100; i++ {

        // Sends continuously

        test <- "Iteration num: " + strconv.Itoa(i)

        time.Sleep(time.Second * 5)

    }

    close(test)

}


func readChan(wg *sync.WaitGroup) {

    defer wg.Done()

    var message string

    var ok bool

    // Reciever deques the value as soon as it recieves it

    // But might take time to proceed

    for {

        select {

        case message, ok = <-test:

            // If channel is closed

            if ok == false {

                return

            }

            log.Println(message)

        default:

            log.Println(message)

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-04-26
  • 2 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)