您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

golang协程如何关闭

2024/5/3 1:49:53发布10次查看
1、通过channel传递退出信号
channel作为go的一种基本数据类型,它有3种基本状态:nil、open、closed。
通过channel共享数据,而不是通过共享内存共享数据。主流程可以通过channel向任何goroutine发送停止信号,就像下面这样:
func run(done chan int) {        for {                select {                case <-done:                        fmt.println(exiting...)                        done <- 1                        break                default:                }                 time.sleep(time.second * 1)                fmt.println(do something)        }} func main() {        c := make(chan int)         go run(c)         fmt.println(wait)        time.sleep(time.second * 5)         c <- 1        <-c         fmt.println(main exited)}
2、使用waitgroup
通常情况下,我们像下面这样使用waitgroup:
1、创建一个waitgroup的实例,假设此处我们叫它wg
2、在每个goroutine启动的时候,调用wg.add(1),这个操作可以在goroutine启动之前调用,也可以在goroutine里面调用。当然,也可以在创建n个goroutine前调用wg.add(n)
3、当每个goroutine完成任务后,调用wg.done()
4、在等待所有goroutine的地方调用wg.wait(),它在所有执行了wg.add(1)的goroutine都调用完wg.done()前阻塞,当所有goroutine都调用完wg.done()之后它会返回。
示例:
type service struct {        // other things         ch        chan bool        waitgroup *sync.waitgroup} func newservice() *service { s := &service{                // init other things                ch:        make(chan bool),                waitgroup: &sync.waitgroup{}, }  return s} func (s *service) stop() {        close(s.ch)        s.waitgroup.wait()} func (s *service) serve() {        s.waitgroup.add(1)        defer s.waitgroup.done()         for {                select {                case <-s.ch:                        fmt.println(stopping...)                        return                default:                }                s.waitgroup.add(1)                go s.anotherserver() }}func (s *service) anotherserver() {        defer s.waitgroup.done()        for {                select {                case <-s.ch:                        fmt.println(stopping...)                        return                default:                }                 // do something        }} func main() {         service := newservice()        go service.serve()         // handle sigint and sigterm.        ch := make(chan os.signal)        signal.notify(ch, syscall.sigint, syscall.sigterm)        fmt.println(<-ch)         // stop the service gracefully.        service.stop()}
更多golang知识请关注golang教程栏目。
以上就是golang协程如何关闭的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录