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

百度站长电脑版市场营销毕业论文5000字

百度站长电脑版,市场营销毕业论文5000字,专业提供网站建设服务公司,百度网站公司信息推广怎么做的文章目录 互斥锁: sync.Mutexsync.WaitGroup 计数器例子func (*WaitGroup) Addfunc (*WaitGroup) Donefunc (*WaitGroup) Wait 读写互斥锁参考资料 临界区总是需要通过同步机制进行保护的,否则就会产生竞态条件,导致数据不一致。 互斥锁&…

文章目录

  • 互斥锁: sync.Mutex
  • sync.WaitGroup 计数器
    • 例子
    • func (*WaitGroup) Add
    • func (*WaitGroup) Done
    • func (*WaitGroup) Wait
  • 读写互斥锁
  • 参考资料

临界区总是需要通过同步机制进行保护的,否则就会产生竞态条件,导致数据不一致。

互斥锁: sync.Mutex

一个互斥锁可以被用来保护一个临界区,我们可以通过它来保证在同一时刻只有一个 goroutine 处于该临界区之内(同一个时刻只有一个线程能够拿到锁)

先通过一个并发读写的例子演示一下,当多线程同时访问全局变量时,结果会怎样?

package mainimport ("fmt"
)var count intfunc main() {for i := 0; i < 2; i++ {go func() {for i := 1000000; i > 0; i-- {count++}fmt.Println(count)}()}fmt.Scanf("\n") //等待子线程全部结束
}//运行结果
//1003065
//1033207

修改代码,在累加的地方添加互斥锁,就能保证我们每次得到的结果都是想要的值

package mainimport ("fmt""sync"
)var (count intlock  sync.Mutex
)func main() {for i := 0; i < 2; i++ {go func() {for i := 1000000; i > 0; i-- {lock.Lock()count++lock.Unlock()}fmt.Println(count)}()}fmt.Scanf("\n") //等待子线程全部结束
}// 运行结果
//1991307
//2000000

每当有 goroutine 想进入临界区时,都需要先对它进行锁定,并且,每个 goroutine 离开临界区时,都要及时地对它进行解锁,锁定和解锁操作分别通过互斥锁 sync.Mutex 的 Lock 和 Unlock 方法实现。使用互斥锁的时候有以下注意事项:

  • 不要重复锁定互斥锁;
  • 不要忘记解锁互斥锁,必要时使用 defer 语句;
  • 不要对尚未锁定或者已解锁的互斥锁解锁;
  • 不要在多个函数之间直接传递互斥锁。

sync.WaitGroup 计数器

type WaitGroup struct {// contains filtered or unexported fields// 包含已筛选或未导出的字段
}

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.

WaitGroup等待goroutines的集合完成。主goroutine调用Add来设置要等待的goroutine的数量。然后,每个goroutine都会运行,并在完成时调用Done。同时,可以使用Wait来阻止,直到所有goroutine都完成。

A WaitGroup must not be copied after first use.
首次使用后不得复制WaitGroup。

In the terminology of the Go memory model, a call to Done “synchronizes before” the return of any Wait call that it unblocks.
在Go内存模型的术语中,对Done的调用在其取消阻止的任何Wait调用返回之前“同步”。

例子

This example fetches several URLs concurrently, using a WaitGroup to block until all the fetches are complete.
此示例同时获取多个URL,使用WaitGroup进行阻止,直到所有获取完成。

package mainimport ("sync"
)type httpPkg struct{}func (httpPkg) Get(url string) {}var http httpPkgfunc main() {var wg sync.WaitGroupvar urls = []string{"http://www.csdn.net/","http://www.youku.com/","http://www.baidu.com/",}for _, url := range urls {// Increment the WaitGroup counter.// 增加WaitGroup计数器。wg.Add(1)// Launch a goroutine to fetch the URL.// 启动goroutine以获取URL。go func(url string) {// Decrement the counter when the goroutine completes.// goroutine完成时递减计数器。defer wg.Done()// Fetch the URL.// 获取URL。http.Get(url)}(url)}// Wait for all HTTP fetches to complete.// 等待所有HTTP获取完成。wg.Wait()
}

func (*WaitGroup) Add

func (wg *WaitGroup) Add(delta int)

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.

Add向WaitGroup计数器添加可能为负数的delta。如果计数器变为零,则会释放在Wait上阻止的所有goroutines。如果计数器为负数,Add会恐慌。

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.

通常,这意味着对Add的调用应该在创建goroutine或其他待等待事件的语句之前执行。如果重用一个WaitGroup来等待多个独立的事件集,则必须在所有以前的wait调用都返回后进行新的Add调用。请参阅WaitGroup示例。

func (*WaitGroup) Done

func (wg *WaitGroup) Done()

Done decrements the WaitGroup counter by one.
Done将WaitGroup计数器递减一。

func (*WaitGroup) Wait

func (wg *WaitGroup) Wait()

Wait blocks until the WaitGroup counter is zero.
等待块,直到WaitGroup计数器为零。

读写互斥锁

互斥锁是完全互斥的,但是有很多实际的场景下是读多写少的,当我们并发的去读取一个资源不涉及资源修改的时候是没有必要加锁的,这种场景下使用读写锁是更好的一种选择。读写锁在Go语言中使用sync包中的RWMutex类型。

读写锁分为两种:读锁和写锁。当一个goroutine获取读锁之后,其他的goroutine如果是获取读锁会继续获得锁,如果是获取写锁就会等待;当一个goroutine获取写锁之后,其他的goroutine无论是获取读锁还是写锁都会等待。

读写锁示例:

package mainimport ("fmt""sync""time"
)var (x      int64wg     sync.WaitGrouplock   sync.Mutexrwlock sync.RWMutex
)func write() {// lock.Lock()   // 加互斥锁rwlock.Lock() // 加写锁x = x + 1time.Sleep(10 * time.Millisecond) // 假设读操作耗时10毫秒rwlock.Unlock()                   // 解写锁// lock.Unlock()                     // 解互斥锁wg.Done()
}func read() {// lock.Lock()                  // 加互斥锁rwlock.RLock()               // 加读锁time.Sleep(time.Millisecond) // 假设读操作耗时1毫秒rwlock.RUnlock()             // 解读锁// lock.Unlock()                // 解互斥锁wg.Done()
}func main() {start := time.Now()for i := 0; i < 10; i++ {wg.Add(1)go write()}for i := 0; i < 1000; i++ {wg.Add(1)go read()}wg.Wait()end := time.Now()fmt.Println(end.Sub(start))
}// 173.3553ms

参考资料

sync包
golang并发之sync包

http://www.khdw.cn/news/34220.html

相关文章:

  • 只做原创内容平台网站百度网页广告怎么做
  • 网站制作学什么软件有哪些推广页面制作
  • 杭州品牌网站开发b站入口2024已更新
  • 简单电商网站模板淮北seo排名
  • 郑州专业网站制作服务费用怎样制作一个自己的网站
  • 河南seo网站开发百度推广关键词规划师
  • 为什么做的网站别的浏览器打不开怎么办百度怎么打广告
  • 城市建设服务中心网站企业管理培训课程网课
  • 招商门户网站建设方案如何做网站推广优化
  • 怎么用nat做网站百度提问
  • 中国进口贸易网官网seo搜索引擎优化课程
  • 江苏连云港网站制作公司网络营销工资一般多少
  • 网站后台管理系统怎么做的如何线上推广引流
  • 网站页面字体设置电商运营公司
  • 网站建设服务流程长沙快速排名优化
  • 做我女朋友程序网站百度软件应用中心
  • 东莞优化网站建设关键词查询工具软件
  • 合肥网站建设过程百度seo推广优化
  • 网站制作什么语言最好百度怎么发广告
  • 青岛新闻最新消息黑帽seo365t技术
  • 网站建设技百度知道app
  • 三优科技 网站开发南京网络推广公司排名
  • 哪里建设品牌网站seo零基础入门到精通200讲
  • wordpress 评论go跳转合肥seo整站优化网站
  • 适合晚上自己看的b站软件大全网站搭建流程
  • 如何在电子表格上做网站的连接软文推广公司
  • 公司网站建设计划新泰网站seo
  • 专门做汽车gps贷款网站只要做好关键词优化
  • 包装设计网站有哪些谷歌搜索引擎营销
  • 即墨网站建设在哪旺道优化软件