Go编程语言相互并发执行 [英] Go Programming Language Mutual Concurrent Execution

查看:195
本文介绍了Go编程语言相互并发执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个并发的例程如下,

 例程1 {

例程过程

关键部分{
}

例行程序
}

例行2 {

例行程序

关键部分{
}

例行程序
}

是否有可能通过使用一些内置函数来实现关键部分?

解决方案

你的问题:


我有N个并发的go例程(或多或少有相同的用途)。每个人都有一个关键部分。在进入关键部分之前,每个例程都会做一些消息发送工作。当它进入关键部分时,我需要所有其他例程必须停止执行,直到退出关键部分。是否有可能通过在GO中使用任何库函数?


你在问什么(强制停止所有其他的goroutine goroutine在关键部分)在Go程序中并不典型。没有库函数可以停止所有其他goroutines,因此您需要通过在程序中的goroutine之间设计适当的同步来阻止它们。典型的场景是,所有的goroutine可以(可能)并发运行,除了那些被阻塞的goroutine外。



控制Go程序中对共享资源的并发访问您可以使用Go频道,sync软件包,管道或网络连接。 使用 sync.Mutex ,Go代码可能看起来像这样(但请记住,Go程序应该尽可能使用Go频道而不是互斥量):

 包主

导入同步

var m sync.Mutex
var wg sync.WaitGroup

func routine1(){
...做某事...

m.Lock()
... critical部分(在这里访问共享资源)...
m.Unlock()

...做某事...
wg.Done()
}

func routine2(){
...做某事...

m.Lock()
...临界区(访问这里共享资源)...
m.Unlock()

...做某事...
wg.Done()
}

func main(){
wg.Add(1);去例程1()
wg.Add(1);去例程2()
wg.Wait()
}


I have two concurrent go routines like below,

Routine 1{                                                  

routine procedure   

critical section{                                                     
}

routine procedure                        
} 

Routine 2{                                                  

routine procedure   

critical section{                                                     
}

routine procedure                       
} 

Is it possible by using some go inbuilt functions to implement critical section ?

解决方案

Your question:

I have N concurrent go routines (all more or less same purpose). Each of those have a critical section. Before entering critical section, each routine just do some message sending job. When it enters critical section, I need all other routines must stop execution until it exits critical section. Is it possible by using any library function in GO?

What you are asking about (to force a stop on all other goroutines while one goroutine is in the critical section) is not typical in Go programs. There is no library function to stop all other goroutines, so you will need to stop them by designing proper synchronization among goroutines in your program. The typical scenario is for all goroutines to (potentially) run concurrently, except for those goroutines that are blocked somehow.

To control concurrent access to a shared resource in a Go program you can use Go channels, the "sync" package, pipes, or network connections.

Using sync.Mutex, the Go code may look like this (but please keep in mind that, whenever possible, Go programs should preferably use Go channels instead of mutexes):

package main

import "sync"

var m sync.Mutex
var wg sync.WaitGroup

func routine1() {
    ... do something ...

    m.Lock()
    ... critical section (access the shared resource here) ...
    m.Unlock()

    ... do something ...
    wg.Done()
}

func routine2() {
    ... do something ...

    m.Lock()
    ... critical section (access the shared resource here) ...
    m.Unlock()

    ... do something ...
    wg.Done()
}

func main() {
    wg.Add(1); go routine1()
    wg.Add(1); go routine2()
    wg.Wait()
}

这篇关于Go编程语言相互并发执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆