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

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

如何判斷一個 goroutine 是否成功或所有 goroutines 都已完成?

如何判斷一個 goroutine 是否成功或所有 goroutines 都已完成?

Go
慕勒3428872 2022-12-19 10:39:29
我正在嘗試使用 DFS 通過檢查圖中每個節(jié)點的循環(huán)來檢查循環(huán)圖。我想為每個節(jié)點運行一個 goroutine,并在檢測到第一個循環(huán)或沒有循環(huán)時終止。在檢測到第一個周期時終止似乎很好,但我無法將其與不再有節(jié)點的情況混合使用。下面是一個似乎有效的實現(xiàn),但對我來說看起來很糟糕,我一直在尋找更好的實現(xiàn)。我基本上使用緩沖通道并在每次我沒有得到一個循環(huán)時遞增一個計數(shù)器,并在計數(shù)器達到底層圖形的大小時終止。使用計數(shù)器來確定我們何時完成對我來說似乎不合時宜?;蛘?,更確切地說,很高興知道在 Go 中是否有更慣用的方法來執(zhí)行此操作。func (c *cycleDet) hasCycle() bool {    adj := c.b // adjacency list representation of the unerlying graph    hasCycles := make(chan bool, len(adj))    for node := range adj {        go func(no nodeID, co chan<- bool) {            visited := make(map[nodeID]struct{})            // c.nodeHasCycle will use a recursive implementation of DFS to            // find out if the node no leads to a cycle.            if c.nodeHasCycle(no, visited) {                co <- true                return            }            co <- false        }(node, hasCycles)    }    var i int    for {        select {        case v := <-hasCycles:            if v {                fmt.Println("got cycle")                return true            }            if i == len(c.b) {                return false            }            i++        }    }}我還遇到了另一個推薦使用“觀察者”goroutine 的 SO 帖子(盡管我找不到 SO 帖子)。我不確定那會是什么樣子,但想象一下它會WaitGroup像這樣與 s 混合:func (c *cycleDet) hasCycle() bool {    hasCycle := make(chan bool)    done := make(chan struct{})    var wg sync.WaitGroup    adj := c.b // adjacency list representation of the unerlying graph    for node := range adj {        go func(no nodeID, co chan<- bool) {            // Use wg to synchronize termination of read-only goroutines.            wg.Add(1)            defer wg.Done()            visited := make(map[nodeID]struct{})            // c.nodeHasCycle will use a recursive implementation of DFS to            // find out if the node no leads to a cycle.            if c.nodeHasCycle(no, visited) {                co <- true                return            }        }(node, hasCycle)    }然而,這種方法讓我擔心,因為我需要睡覺讓觀察者只在第一個WaitGroup計數(shù)器遞增后才開始等待,這似乎比第一個解決方案脆弱得多。
查看完整描述

1 回答

?
HUH函數(shù)

TA貢獻1836條經(jīng)驗 獲得超4個贊

你是對的,這WaitGroup可能就是你想要的。但是,您沒有正確使用它。首先,您需要在wg.Add(1)調(diào)用wg.Done(). 其次,調(diào)用wg.Wait()塊直到等待組中的所有 go routines 完成執(zhí)行,所以你不希望它并發(fā)執(zhí)行。


至于短路,確實沒有什么好辦法。我的建議是使用上下文。在這種情況下,您必須做的一件事是將上下文連接到您的調(diào)用中,nodeHasCycle如果您想要真正的短路行為。


修復你的代碼,我們有:


func (c *cycleDet) hasCycle() bool {

    ctx, cancel := context.WithCancel(context.Background())

    hasCycle := make(chan bool)

    var wg sync.WaitGroup


    adj := c.b // adjacency list representation of the unerlying graph

    for node := range adj {

        wg.Add(1)

        go func(ctx context.Context, no nodeID, co chan<- bool, cancel context.CancelFunc) {

            // Use wg to synchronize termination of read-only goroutines.

            defer wg.Done()


            select {

                case <-ctx.Done():

                    return

                default:

            }


            visited := make(map[nodeID]struct{})

            // c.nodeHasCycle will use a recursive implementation of DFS to

            // find out if the node no leads to a cycle.

            if c.nodeHasCycle(ctx, no, visited) {

                co <- true

                cancel()

                return

            }

        }(ctx, node, hasCycle, cancel)

    }


    // Observer goroutine to notify when wg is done waiting.

    time.Sleep(100 * time.Millisecond)

    wg.Wait()

    defer cancel()


    select {

    case <-hasCycle:

        fmt.Println("got a cycle")

        return true

    default:

        fmt.Println("no cycle detected")

        return false

    }

}

通過這種方式設置,您可以確保對 go-routine 的所有調(diào)用都將運行,除非找到一個循環(huán),在這種情況下,不會調(diào)用其他 go-routes 并且,如果您添加邏輯以檢查是否取消nodeHasSycle,則您也可以停止對任何正在運行的調(diào)用的執(zhí)行。


查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號