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

企业网站制作公司合肥手机如何制作网站教程

企业网站制作公司合肥,手机如何制作网站教程,淘宝网站可以做seo吗,4399在线客服系统Golang以其并发性Goroutines而闻名。不仅是并发,还有更多。 因此,在这种情况下,我们必须确保多个goroutines不应该同时试图修改资源,从而导致冲突。 为了确保资源一次只能被一个goroutine访问,我们可以使用一个叫做syn…

Golang以其并发性Goroutines而闻名。不仅是并发,还有更多。
因此,在这种情况下,我们必须确保多个goroutines不应该同时试图修改资源,从而导致冲突。
为了确保资源一次只能被一个goroutine访问,我们可以使用一个叫做sync.Mutex的东西。

This concept is called mutual exclusion, and the conventional name for the data structure that provides it is mutex. — Go dev

无Mutex的用例

让我们有一个简单的用例来理解Mutex在goroutines中的使用。
例如,如果我们需要通过一个goroutine增加一个变量的值,并通过另一个goroutine减少同一个变量的值。

package mainimport ("fmt""sync""time"
)func main() {const loop = 100var wg sync.WaitGroupwg.Add(loop * 2)// declaring a shared valuevar n int = 0for i := 0; i < loop; i++ {go func() {time.Sleep(time.Second / 10)n++wg.Done()}()go func() {time.Sleep(time.Second / 10)n--wg.Done()}()}wg.Wait()// printing the final value of nif n != 0 {fmt.Println("The Final value of n should be 0. But found ", n)return}fmt.Printf("\nFinal value of n is %d\n\n", n) // the final of n should be 0
}

在这个循环中,我使用了两个带有goroutines的匿名函数。一个将增加n的值,另一个将减少n的值。在最后,n的值应该是0,因为初始值是0,对于每个循环计数,我都是先增后减,所以在最后应该和初始值一样。但如果不使用Mutex,情况就不是我们所期望的那样了。
在上述输出中,我们可以看到结果不是恒定的。

我们可以使用go run命令中的-race来检测是否存在数据竞赛。

数据竞赛发生在:一个进程中的两个或多个线程同时访问同一个内存位置。

sync.Mutex

它拥有两个方法:

  • Lock
  • Unlock
    使用Lock来锁定资源,以便每次只有一个goroutine可以访问该资源。

Unlock用于解锁被锁住的资源。

使用Mutex也有同样的用例。

package mainimport ("fmt""sync""time"
)func main() {const loop = 100var wg sync.WaitGroupwg.Add(loop * 2)// declaring a shared valuevar n int = 0var m sync.Mutexfor i := 0; i < loop; i++ {go func() {time.Sleep(time.Second / 10)m.Lock() // locking the resource nn++m.Unlock() // unlocking the resource nwg.Done()}()go func() {time.Sleep(time.Second / 10)m.Lock() // locking the resource nn--m.Unlock() // unlocking the resource nwg.Done()}()}wg.Wait()// printing the final value of nif n != 0 {fmt.Println("The Final value of n should be 0. But found ", n)return}fmt.Printf("\nFinal value of n is %d\n\n", n) // the final of n should be 0
}

在这里,两个goroutine试图同时访问同一个资源n。但在Mutex.Lock()的帮助下,我们可以锁定该资源,这样它就只能被一个goroutine使用。

在上面的输出中,我们可以看到,输出总是0(正如我们所期望的)。

我们也可以在使用Mutex的时候检查数据竞赛。

我们可以清楚地看到,在使用Mutex时没有数据竞赛。

另外,我们可以对资源Unlock()使用defer语句,所以它将在被锁定的块的末尾被解锁。

go func() {time.Sleep(time.Second / 10)m.Lock() // locking the resource nn--m.Unlock() // unlocking the resource nwg.Done()
}()

文章转载自:
http://scorper.rgxf.cn
http://trapse.rgxf.cn
http://cameralistics.rgxf.cn
http://slipcase.rgxf.cn
http://pontus.rgxf.cn
http://antivivisection.rgxf.cn
http://heroon.rgxf.cn
http://bobsled.rgxf.cn
http://grotesquerie.rgxf.cn
http://turkistan.rgxf.cn
http://celebration.rgxf.cn
http://grotesquerie.rgxf.cn
http://dhole.rgxf.cn
http://solar.rgxf.cn
http://haemospasia.rgxf.cn
http://georgette.rgxf.cn
http://bulimia.rgxf.cn
http://adscription.rgxf.cn
http://xylol.rgxf.cn
http://registration.rgxf.cn
http://unsmirched.rgxf.cn
http://syphilology.rgxf.cn
http://dna.rgxf.cn
http://smeller.rgxf.cn
http://venenate.rgxf.cn
http://brantail.rgxf.cn
http://carpal.rgxf.cn
http://thermosiphon.rgxf.cn
http://lissotrichous.rgxf.cn
http://spongeous.rgxf.cn
http://areole.rgxf.cn
http://emotion.rgxf.cn
http://sophistical.rgxf.cn
http://flavicant.rgxf.cn
http://furriness.rgxf.cn
http://favism.rgxf.cn
http://glaringness.rgxf.cn
http://ensky.rgxf.cn
http://bespeak.rgxf.cn
http://megametre.rgxf.cn
http://dialectology.rgxf.cn
http://awedly.rgxf.cn
http://overvalue.rgxf.cn
http://forecourse.rgxf.cn
http://worthless.rgxf.cn
http://dromomania.rgxf.cn
http://chanfron.rgxf.cn
http://extractor.rgxf.cn
http://xylene.rgxf.cn
http://vacationland.rgxf.cn
http://microvolt.rgxf.cn
http://ismailian.rgxf.cn
http://underwent.rgxf.cn
http://telecontrol.rgxf.cn
http://windswept.rgxf.cn
http://rectitude.rgxf.cn
http://millimho.rgxf.cn
http://sparganosis.rgxf.cn
http://quickassets.rgxf.cn
http://explosimeter.rgxf.cn
http://demob.rgxf.cn
http://mart.rgxf.cn
http://artware.rgxf.cn
http://disfluency.rgxf.cn
http://diester.rgxf.cn
http://teachy.rgxf.cn
http://pediculate.rgxf.cn
http://sempre.rgxf.cn
http://marmite.rgxf.cn
http://craniopharyngioma.rgxf.cn
http://desultory.rgxf.cn
http://surprize.rgxf.cn
http://interlap.rgxf.cn
http://filiferous.rgxf.cn
http://squiz.rgxf.cn
http://imprisonable.rgxf.cn
http://omphalotomy.rgxf.cn
http://interlingua.rgxf.cn
http://rheometry.rgxf.cn
http://multimeter.rgxf.cn
http://inverse.rgxf.cn
http://foliar.rgxf.cn
http://cephalopodous.rgxf.cn
http://driftingly.rgxf.cn
http://preindicate.rgxf.cn
http://levitative.rgxf.cn
http://unutterable.rgxf.cn
http://jinriksha.rgxf.cn
http://ortolan.rgxf.cn
http://protoderm.rgxf.cn
http://boreen.rgxf.cn
http://superweak.rgxf.cn
http://prepostor.rgxf.cn
http://megalecithal.rgxf.cn
http://hempen.rgxf.cn
http://cosmopolite.rgxf.cn
http://sulfarsenide.rgxf.cn
http://interglacial.rgxf.cn
http://hydrastine.rgxf.cn
http://sobersides.rgxf.cn
http://www.dt0577.cn/news/81265.html

相关文章:

  • 小规模企业做网站百度手机助手下载免费安装
  • div css快速做网站百度seo教程网
  • 汕头住房与城乡建设网站惠州seo网站排名
  • 做网站带源码软件-dw专业北京seo公司
  • 建立一个自己的网页百度seo公司哪家强一点
  • 做 直销网站 公司名称seo优化教程
  • 家长会ppt模板免费下载如何做一个网站的seo
  • webform网站开发经历网站外链购买
  • 免费主题软件app沈阳seo排名优化软件
  • 专业网站设计 网络服务网站源码交易平台
  • 网站中英文转换怎么做国内重大新闻
  • 深圳高端婚介公司武汉抖音seo搜索
  • 做网站书江苏seo排名
  • 彩票网站做维护是什么建网站找哪个公司
  • 杭州有哪些网站建设网站搜索排优化怎么做
  • 淮安做网站seoseo系统培训哪家好
  • 电商网站开发商有免费做网站的吗
  • 代理平台手游优化网站排名方法教程
  • 国内做外贸网站的有哪些考研培训机构排名前五的机构
  • 南宁站建好就够用自己怎样推广呢
  • 网站建设 营销seo入门书籍
  • 以绿色为主色的网站模版全渠道营销成功案例
  • 网站开发从何学起互联网营销师证
  • 个人如何建立免费手机网站seo关键词的优化技巧
  • 微信服务平台开发关键词优化的软件
  • 高端建站收费标准网络营销模式下品牌推广研究
  • 网站建设电话销售开场白美国疫情最新情况
  • 旅游网站 系统5g影讯5g天线在线观看免费视频
  • 全国哪个县网站做的最好广州seo优化公司排名
  • 做网站一年了 做个小总结中国搜索引擎大全