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

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

這個空的 select-case-default 代碼塊有什么影響?

這個空的 select-case-default 代碼塊有什么影響?

Go
繁花如伊 2022-11-08 15:49:14
我試圖理解一個池庫代碼,當(dāng)實(shí)例化一個池結(jié)構(gòu)時,調(diào)用一個名為 的函數(shù)startCleanerLocked(t Duration),在這個函數(shù)中,有一個空select...case...default...代碼塊,我不明白這個代碼塊的效果是什么。Pool Interface是:// Pool interface.type Pool interface {    Get(ctx context.Context) (io.Closer, error)    Put(ctx context.Context, c io.Closer, forceClose bool) error    Close() error}List Struct實(shí)施Pool Interface,type List struct {    // New is an application supplied function for creating and configuring a    // item.    //    // The item returned from new must not be in a special state    // (subscribed to pubsub channel, transaction started, ...).    New func(ctx context.Context) (io.Closer, error)    // mu protects fields defined below.    mu     sync.Mutex    cond   chan struct{}    closed bool    active int    // clean stale items    cleanerCh chan struct{}    // Stack of item with most recently used at the front.    idles list.List    // Config pool configuration    conf *Config}創(chuàng)建新池時,startCleanerLocked(t Duration)調(diào)用函數(shù):// NewList creates a new pool.func NewList(c *Config) *List {    // check Config    if c == nil || c.Active < c.Idle {        panic("config nil or Idle Must <= Active")    }    // new pool    p := &List{conf: c}    p.cond = make(chan struct{})    p.startCleanerLocked(time.Duration(c.IdleTimeout))    return p}在 中startCleanerLocked(t Duration),有一個select...case...default:// startCleanerLockedfunc (p *List) startCleanerLocked(d time.Duration) {    if d <= 0 {        // if set 0, staleCleaner() will return directly        return    }    if d < time.Duration(p.conf.IdleTimeout) && p.cleanerCh != nil {        select {        case p.cleanerCh <- struct{}{}:        default:        }    }    // run only one, clean stale items.    if p.cleanerCh == nil {        p.cleanerCh = make(chan struct{}, 1)        go p.staleCleaner()    }}這個代碼塊的效果是什么:select {    case p.cleanerCh <- struct{}{}:    default:}好像沒什么可做的...
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個贊

select {

case p.cleanerCh <- struct{}{}:

default:

}

這是一個非阻塞select語句。(因?yàn)橛衐efault:案例)


如果在通道的另一端有一個接收器 goroutine p.cleanerCh,即當(dāng)前有一個 goroutine 在接收操作中“等待” <-p.cleanerCh,則case p.cleanerCh <- struct{}{}立即執(zhí)行它,這有效地解除了接收操作<-p.cleanerCh的阻塞,然后 goroutine 可以繼續(xù)執(zhí)行任何操作聲明如下。


如果沒有接收者 goroutine,那么default:case 會立即執(zhí)行,并且周圍的startCleanerLocked函數(shù)可以繼續(xù)執(zhí)行語句后面的任何select語句。


select {

case <-ticker.C:

case <-p.cleanerCh: // maxLifetime was changed or db was closed.

}

這是一個阻止select聲明。(因?yàn)闆]有default:案例)


該select語句阻塞for循環(huán),直到兩個通信案例之一準(zhǔn)備好接收。


查看完整回答
反對 回復(fù) 2022-11-08
  • 1 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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