golang并发编程实践 -- 简单生产者消费者(with lock)
上一篇文章用golang中的channel实现了简单的消费者模型,下面的版本是用传统的锁技术实现的版本,相对比会发现golang提供的channel更好用。而且golang的channel可以完成很多在别的语言里需要很多代码才能实现的功能。以后陆续解答。 package main import ( "fmt" "sync" "time" ) type Queue struct { Elem []int Capacity int Front int Rear int Lock sync.Locker Cond *sync.Cond } func New() *Queue { theQueue := &Queue{} theQueue.Capacity = 10 theQueue.Elem = ...阅读全文