当前位置: 首页 > wzjs >正文

凡科网站的排名做不上去刷赞抖音推广网站

凡科网站的排名做不上去,刷赞抖音推广网站,为什么网站建设要将access数据库文件变成asa,mac wordpress客户端在 Go 语言里,sync 包中的 WaitGroup 是一个实用工具,用于等待一组 goroutine 完成任务。其核心原理是通过内部维护一个计数器,该计数器初始值为 0,每启动一个新的 goroutine 就将计数器加 1,每个 goroutine 完成任务后…

在 Go 语言里,sync 包中的 WaitGroup 是一个实用工具,用于等待一组 goroutine 完成任务。其核心原理是通过内部维护一个计数器,该计数器初始值为 0,每启动一个新的 goroutine 就将计数器加 1,每个 goroutine 完成任务后会将计数器减 1,当计数器变为 0 时,意味着所有 goroutine 都已完成任务。

下面为你展示 WaitGroup 的使用示例:

package mainimport ("fmt""sync"
)var (counter intmutex   sync.Mutex
)func increment() {mutex.Lock()defer mutex.Unlock()counter++
}func main() {var wg sync.WaitGroupnumGoroutines := 1000wg.Add(numGoroutines)for i := 0; i < numGoroutines; i++ {go func() {defer wg.Done()increment()}()}wg.Wait()wg.Add(1)fmt.Println("Counter:", counter)
}

代码解释

  1. sync.WaitGroup 的声明:在 main 函数里,借助 var wg sync.WaitGroup 声明了一个 WaitGroup 实例。
  2. wg.Add 方法:在启动每个 worker goroutine 之前,调用 wg.Add(1) 把计数器的值加 1,以此表明有一个新的 goroutine 开始工作。
  3. defer wg.Done():在 worker 函数中,使用 defer wg.Done() 保证当该函数执行结束时,计数器的值会减 1。
  4. wg.Wait 方法:在 main 函数里调用 wg.Wait(),此操作会让 main 函数阻塞,直至计数器的值变为 0,也就是所有的 goroutine 都已完成任务。

注意事项

  • 要保证 wg.Add 方法在 goroutine 启动前调用,wg.Done 方法在 goroutine 结束时调用。
  • 不能在 wg.Wait 调用之后再调用 wg.Add,否则会引发恐慌(panic),实际上高版本并没有这个问题。
  • WaitGroup 是按值传递的,若要在多个 goroutine 中使用,需传递其指针。

底层原理实现

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.package syncimport ("internal/race""sync/atomic""unsafe"
)// A WaitGroup waits for a collection of goroutines to finish.
// The main goroutine calls [WaitGroup.Add] to set the number of
// goroutines to wait for. Then each of the goroutines
// runs and calls [WaitGroup.Done] when finished. At the same time,
// [WaitGroup.Wait] can be used to block until all goroutines have finished.
//
// A WaitGroup must not be copied after first use.
//
// In the terminology of [the Go memory model], a call to [WaitGroup.Done]
// “synchronizes before” the return of any Wait call that it unblocks.
//
// [the Go memory model]: https://go.dev/ref/mem
type WaitGroup struct {noCopy noCopystate atomic.Uint64 // high 32 bits are counter, low 32 bits are waiter count.sema  uint32
}// Add adds delta, which may be negative, to the [WaitGroup] counter.
// If the counter becomes zero, all goroutines blocked on [WaitGroup.Wait] are released.
// If the counter goes negative, Add panics.
//
// Note that calls with a positive delta that occur when the counter is zero
// must happen before a Wait. Calls with a negative delta, or calls with a
// positive delta that start when the counter is greater than zero, may happen
// at any time.
// Typically this means the calls to Add should execute before the statement
// creating the goroutine or other event to be waited for.
// If a WaitGroup is reused to wait for several independent sets of events,
// new Add calls must happen after all previous Wait calls have returned.
// See the WaitGroup example.
func (wg *WaitGroup) Add(delta int) {if race.Enabled {if delta < 0 {// Synchronize decrements with Wait.race.ReleaseMerge(unsafe.Pointer(wg))}race.Disable()defer race.Enable()}state := wg.state.Add(uint64(delta) << 32)v := int32(state >> 32)w := uint32(state)if race.Enabled && delta > 0 && v == int32(delta) {// The first increment must be synchronized with Wait.// Need to model this as a read, because there can be// several concurrent wg.counter transitions from 0.race.Read(unsafe.Pointer(&wg.sema))}if v < 0 {panic("sync: negative WaitGroup counter")}if w != 0 && delta > 0 && v == int32(delta) {panic("sync: WaitGroup misuse: Add called concurrently with Wait")}if v > 0 || w == 0 {return}// This goroutine has set counter to 0 when waiters > 0.// Now there can't be concurrent mutations of state:// - Adds must not happen concurrently with Wait,// - Wait does not increment waiters if it sees counter == 0.// Still do a cheap sanity check to detect WaitGroup misuse.if wg.state.Load() != state {panic("sync: WaitGroup misuse: Add called concurrently with Wait")}// Reset waiters count to 0.wg.state.Store(0)for ; w != 0; w-- {runtime_Semrelease(&wg.sema, false, 0)}
}// Done decrements the [WaitGroup] counter by one.
func (wg *WaitGroup) Done() {wg.Add(-1)
}// Wait blocks until the [WaitGroup] counter is zero.
func (wg *WaitGroup) Wait() {if race.Enabled {race.Disable()}for {state := wg.state.Load()v := int32(state >> 32)w := uint32(state)if v == 0 {// Counter is 0, no need to wait.if race.Enabled {race.Enable()race.Acquire(unsafe.Pointer(wg))}return}// Increment waiters count.if wg.state.CompareAndSwap(state, state+1) {if race.Enabled && w == 0 {// Wait must be synchronized with the first Add.// Need to model this is as a write to race with the read in Add.// As a consequence, can do the write only for the first waiter,// otherwise concurrent Waits will race with each other.race.Write(unsafe.Pointer(&wg.sema))}runtime_Semacquire(&wg.sema)if wg.state.Load() != 0 {panic("sync: WaitGroup is reused before previous Wait has returned")}if race.Enabled {race.Enable()race.Acquire(unsafe.Pointer(wg))}return}}
}
http://www.dtcms.com/wzjs/167701.html

相关文章:

  • 网站别人做的我自己怎么续费游戏优化大师
  • 网站做优化应该具备什么北京建设网站公司
  • 公司做网站那家好seo运营招聘
  • 深圳网站建设补助广州最新疫情情况
  • 海淘哪些网站做攻略好怎么做推广和宣传
  • 如何做情趣网站seo关键词优化推广外包
  • 兰州网站优化seo研究中心晴天
  • 免费网站建设程序下载单页网站排名优化
  • wordpress魔板优化关键词的正确方法
  • 绍兴柯桥哪里有做网站的网络营销推广平台有哪些
  • 商梦建站百度浏览官网
  • 免费网站申请域名澳门seo知识分享
  • 沙洋网站定制站点查询
  • 国内网站设计案例欣赏常用的网络推广方式有哪些
  • 深圳网站设计制作公司 维仆网络营销策划书的范文
  • 广州市中智软件开发有限公司seo优化报价公司
  • 建设网站怎么做站长之家权重查询
  • 做网站需要相机吗刷网站seo排名软件
  • 电子商务网站建设学什么做一个公司网站要多少钱
  • 医疗器械网上商城优化大师怎么样
  • 如何做网站劫持广州最新重大新闻
  • 上海网站制作网站开发口碑推广
  • 自己做网站 搜索功能开发国际大新闻最新消息
  • 网站可以做软件检测吗佛山seo
  • 做网站客户改来改去手机百度高级搜索入口在哪里
  • 口碑好的网站建设哪家好关键词怎么提取
  • 美联社中文新闻福建搜索引擎优化
  • 建设网站技术人员先进事迹最新军事新闻 今日 最新消息
  • 山东济南网站建设十大外贸平台
  • 小型网站怎样优化中文搜索引擎排名