go语言控制goroutine协程退出的2种方法总结

我们知道,在go语言中,goroutine的执行会随着main线程的退出而终结, 即如果main线程退出,则所有的goroutine都会被强制退出,不管你是否已经执行完毕。

如果我们希望main进程等待所有的goroutine执行完毕后再退出,则可以有3种方式来实现,具体如下:

1. 使用go标准库sync中提供的 sync.WaitGroup里面提供的Add, Done, Wait方法;

package mainimport ("fmt""sync""time"
)
// 专业企业信息化软件定制开发 免费咨询 https://dev.tekin.cn/contactus.html
var wg sync.WaitGroup // 定义全局变量wg类型是sync.WaitGroup结构体, 因为我们要使用的方法是绑定在这个结构体上的func test(n int) {defer wg.Done() //协程每次完成后执行这个将计数增量 -1; 注意这个代码被调用的次数要和wg.Add(delta)这里设置的增量一致for i := 1; i <= n; i++ {fmt.Printf("test %v \n", i)time.Sleep(100 * time.Millisecond)}}func main() {wg.Add(2) // 增加变量质量, 这里的数字是你后面要启动几个协程就写几, 如要起2个协程就写 2, 这里的数字有1个余量 即0, 所以如果是2 则wg.Done()最多可执行3次, 超过3次就会报panic异常, 如果 wg.Done()只执行1次则会报死锁异常!!!go test(10)go test(5)test(6) // 这个正常 因为wg源码里面的增量比较是 < 0 所以//test(7) //这个会异常了 因为上面的的delta增量为2for i := 0; i < 10; i++ {fmt.Printf("main %v\n", i)}// 这里会阻塞主进程等待所有的协程执行完毕后才会退出wg.Wait()
}

2.  利用管道chan读取时会一直阻塞当前线程的特性实现等待

package mainimport "fmt"
// 专业企业信息化软件定制开发 免费咨询 https://dev.tekin.cn/contactus.html
// 只读/只写 chan使用示例
// 发送消息 ch入参为仅写
func Sender(ch chan<- int, exitCh chan struct{}) {for i := 0; i < 10; i++ {ch <- i}close(ch)var a struct{}exitCh <- a
}// Receiver接收端 ch入参仅读
func Receiver(ch <-chan int, exitCh chan struct{}) {//循环for {v, ok := <-chif !ok {break //退出循环}fmt.Println("v=", v)}var a struct{}exitCh <- a
}func main() {// 声明sender chanvar ch = make(chan int, 10)var exitCh = make(chan struct{}, 2)Sender(ch, exitCh)Receiver(ch, exitCh)var total = 0for _ = range exitCh {total++if total == 2 {break}}fmt.Println("结束...")
}

总结

上面2种方式, 第一种实现起来比较简单,可少写一些代码, 但是性能相比第二种方式要低一些,因为第一种方式里面使用了race,原子状态维护和不少unsafe的方法(见后面的WaitGroup源码参考)。 第二种方式代码稍微复杂,但是效率较高,控制也比较灵活。

sync.WaitGroup源码参考

// 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 Add to set the number of
// goroutines to wait for. Then each of the goroutines
// runs and calls Done when finished. At the same time,
// 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 Done
// “synchronizes before” the return of any Wait call that it unblocks.
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 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.mzph.cn/pingmian/12627.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【设计模式】JAVA Design Patterns——Acyclic Visitor(非循环访问者模式)

&#x1f50d;目的 允许将新功能添加到现有的类层次结构中&#xff0c;而不会影响这些层次结构&#xff0c;也不会有四人帮访客模式中那样循环依赖的问题。 &#x1f50d;解释 真实世界例子 我们有一个调制解调器类的层次结构。 需要使用基于过滤条件的外部算法&#xff08;是…

奖金+1 万,OpenTenBase 开源核心贡献挑战赛,KB 专家助力其跑在 K8s 上

OpenTenBase 是由开放原子开源基金会孵化及运营的开源项目&#xff0c;是一款开放中立的企业级分布式 HTAP 开源数据库。OpenTenBase 具备高扩展性、商业数据库语法兼容、分布式 HTAP 引擎、多级容灾和多维度资源隔离等能力&#xff0c;已成功应用于金融、医疗、航天等行业的核…

FlyFlow:支持驳回后自动跨节点跳回

本周更新 新增&#xff1a;审批节点驳回&#xff08;拒绝配置的驳回&#xff09;支持自动跳回当前节点新增&#xff1a;修改数据节点新增&#xff1a;删除数据节点新增&#xff1a;子流程支持配置自动跳过发起人节点优化&#xff1a;两个项目合并一个单体项目优化&#xff1a;…

Hadoop阶段性技能抽检题,无直接答案但有提示信息

项目名&#xff1a;Hadoop平台及组件的部署管理 考核内容&#xff1a; 考核以大数据技术为核心内容&#xff0c;重点考查同学们基于Hadoop平台环境下&#xff0c;利用Hadoop技术生态组件&#xff0c;综合软件开发相关技术&#xff0c;解决实际问题的能力&#xff0c;所有学生在…

LeetCode hot100-38-Y

226. 翻转二叉树给你一棵二叉树的根节点 root &#xff0c;翻转这棵二叉树&#xff0c;并返回其根节点。这道题莫名其妙做出来了&#xff0c;看评论好多人都是莫名其妙做出来的。就是连续做了几道题有感觉了。很难解释。 我的做法 后序递归 class Solution {public TreeNode i…

LeetCode:2589.完成所有任务的最少时间(贪心 Java)

目录 完成所有任务的最少时间 题目描述&#xff1a; 实现代码与解析&#xff1a; 贪心 原理思路&#xff1a; 完成所有任务的最少时间 题目描述&#xff1a; 你有一台电脑&#xff0c;它可以 同时 运行无数个任务。给你一个二维整数数组 tasks &#xff0c;其中 tasks[i] …

Linux 系统下进程异常的处理方式

Linux 系统异常进程处理 一、僵尸进程 说明&#xff1a;僵尸进程对系统来说就是系统已经接管不了并处于异常状态的进程&#xff0c;既不会自动释放&#xff0c;也不能被系统接管&#xff0c;下面列出几种查看并kill僵尸进程的方式 。 方式一、使用如下命令查看目前系统状态为…

【C语言】水仙花数

问题 水仙花数&#xff08;Narcissistic number&#xff09;也被称为超完全数字不变数&#xff08;pluperfect digital invariant, PPDI&#xff09;、自恋数、自幂数或阿姆斯壮数数&#xff08;Armstrong number&#xff09;。 它是指一个n位数&#xff08;n≥3&#xff09;…

【C++】---继承

【C】---继承 一、继承的概念及定义1、继承的概念2、定义语法格式3、继承基类成员访问方式的变化 二、基类 和 派生类 的对象之间的赋值转换1、赋值规则2、切片&#xff08;1&#xff09;子类对象 赋值 给 父类对象&#xff08;2&#xff09;子类对象 赋值 给 父类指针&#xf…

Promise链式调用与错误处理

Promise链式调用是一种处理异步操作的方法&#xff0c;它可以依次执行多个异步任务&#xff0c;并且可以在每个任务完成后进行后续操作。 在Promise链式调用中&#xff0c;每个任务都返回一个Promise对象&#xff0c;可以通过调用.then()方法来指定任务完成后的操作&#xff0…

Python邮件处理库之flanker使用详解

概要 Flanker是一个开源的邮件处理库,专门设计用于解析、验证和构建电子邮件地址和MIME消息。由Mailgun开发,它旨在提高邮件处理的效率和准确性,尤其适用于需要高效邮件验证和解析的应用程序。 安装 安装Flanker非常简单,可以通过Python的包管理器pip进行安装: pip ins…

MYSQL库管理---1.默认系统库 2.创建/删除/查看库

文章目录 @[TOC](文章目录)MYSQL默认的系统库1、information_schema(数据库)2、mysql3、performance_schema (性能库)4、sysMYSQL创建/删除/查看库MYSQL默认的系统库 1、information_schema(数据库) 1、记录了用户、表、视图等元数据信息。这个库是虚拟出来的库,是由MySQL…

信息流中的混排与流控

待完成. 一. 背景 问题特点: 无法事先拿到所有请求, 离线统一求解. 因此叫 online-matching.应用于在线服务, 求解rt不能高于50ms 二. CIKM 22’, 阿里广告动态定坑 见参考[1]. 2.1 问题建模,动态背包 略, 详见论文 2.2 求解, pidbeam search 思考: beam search 有用的…

Python---Pandas万字总结(2)

DataFrame深度学习 使用 pandas 做数据分析&#xff0c;那么DataFrame一定是被使用得最多的类型&#xff0c;它可以用来保存和处理异质的二维数据。这里所谓的“异质”是指DataFrame中每个列的数据类型不需要相同&#xff0c;这也是它区别于 NumPy 二维数组的地方。DataFrame提…

使用 cloudflare 免费服务,搭建临时邮箱,无需暴露自己的真实邮箱地址,保护个人隐私

使用 cloudflare 免费服务&#xff0c;搭建临时邮箱 地址 在线演示 &#x1f310;Github地址 https://github.com/find-xposed-magisk/cloudflare_temp_email 功能/TODO Cloudflare D1 作为数据库 使用 Cloudflare Pages 部署前端 使用 Cloudflare Workers 部署后端 email 转…

【源码+文档+调试讲解】微信小程序家政项目小程序

摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了微信小程序家政项目小程序的开发全过程。通过分析微信小程序家政项目小程序管理的不足&#xff0c;创建了一个计算机管理微信小程序家政项目小程序的方案。文章介…

每日新闻掌握【2024年5月13日 星期一】

2024年5月13日 星期一 农历四月初六 TOP大新闻 全国23个城市全面取消限购&#xff0c;超50城支持住房“以旧换新” 据统计&#xff0c;截至5月9日&#xff0c;全国共计50余个城市对限购政策松绑&#xff0c;其中西安、成都、杭州、佛山、东莞、厦门、南京、苏州等23个城市全面…

STM32睡眠模式

文章目录 前言PWR介绍电源框图上电复位和掉电复位可编程电压检测器低功耗模式模式选择电源控制寄存器 睡眠模式停止模式待机模式 前言 在单片机产品中&#xff0c;例如遥控这类产品&#xff0c;长时间处于待机状态下&#xff0c;所以对于这类产品在待机时就应该尽可能的减少不…

【环境安装】nodejs 国内源下载与安装以及 npm 国内源配置

前言 Node.js 是一个基于 Chrome V8 引擎构建的 JavaScript 运行时环境&#xff0c;它能够使 JavaScript 在服务器端运行。它拥有强大的包管理器 npm&#xff0c;使开发者能够轻松管理和共享 JavaScript 代码包。 在中国&#xff0c;由于众所周知的原因&#xff0c;我们可能会…