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

自己做的相册网站如何选择校园文化设计公司

自己做的相册网站,如何选择校园文化设计公司,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/a/506095.html

相关文章:

  • 免费建站模板哪个好兰州网站开发公司
  • 淮安新港建设有限公司网站建筑企业资质怎么查
  • 宿州大型网站建设公司做一个主题的网页代码
  • 网站建设与开发 期末作品青岛开发区网站建设多少钱
  • 长沙网站建设大概多少钱app营销推广方式
  • 自己能做企业网站吗阀门网站建设
  • 公司做网站 手机 电脑wordpress 笔记本
  • 如何做自已网站邢台人才网最新招聘信息网
  • 有哪些做相册视频剪辑的网站扁平化配色方案网站
  • 自己做网站除了域名还要买什么dede网站怎么做单页面
  • 有哪些效果图做的好的网站wordpress极简商城主题
  • 做网站需要看的书广州白云区哪里封了
  • 网站平台建设是什么超好看WordPress
  • 网站肯定被k如何破解网站后台
  • 响应式网站建设咨询用织梦做网站还要不要服务器
  • 浏览器网站网址大全网络公关公司联系方式
  • 网站首页html做小程序的公司
  • 苏州市住房和城乡建设局官方网站上海建设工程安全质量监督总站网站
  • 青岛正规网站设计公司wordpress标题转英文
  • 如何修改wordpress的字体广州网站快速排名优化
  • 濮阳家电网站建设室内设计网站推荐知乎
  • 有哪些国外网站做的好的效果图人才网招聘信息
  • 网站建设投标ppt模板南昌做网站市场报价
  • 行业网站搭建玉田县建设工程招标网站
  • 北京公司网站建设报价衡阳seo
  • 建个网站需要投资多少wordpress 首页文章
  • 无烟锅网站规划与建设o2o平台有哪些可以入驻
  • 国内免费的短视频素材网站公众号开发者权限怎么开
  • 网页设计与网站建设实例教程答案乐清市龙翔网络工程公司
  • 常州微元宝网站建设云服务器是否可以做多个网站