Golang -- 使用 Bufferd channel 实现 线程安全的 pool
2015-03-26 23:06
519 查看
从 文章 进行转载,再次感谢
现在是 Golang1.4了,
概述
我们知道,Go 语言已经提供了sync.Pool,但是做的不怎么好,所以有必要来实现一个 Pool
代码
type Pool struct { pool chan *Client } // Create a new Pool func NewPool(max int) *Pool{ return &Pool{ pool: make(chan *Client, max) } } // Get a Client from Pool func (p *Pool) Borrow() *Client { var cl *Client select { case cl = <-p.pool: default: c1 = newClient() } return cl } // Return the Cliet to the pool func (p *Pool) Return(cl *Client){ select { case p.pool<- cl: default: // let it go, let it go.... } }
现在是 Golang1.4了,
sync.Pool还是不可以用吗?
相关文章推荐
- Go语言中使用 buffered channel 实现线程安全的 pool
- 使用Golang下chan实现一个缓冲池(Pool)
- 使用JAVA中的动态代理实现数据库连接池(载自:http://www.ibm.com/developerworks/cn/java/l-connpoolproxy/)
- 使用 ServerSocketChannel 实现的 File 服务器
- 使用boost::circular_buffer_space_optimized实现OTL数据库连接池
- 使用common-pool实现的一个简单的线程池
- 使用boost::circular_buffer_space_optimized实现OTL数据库连接池
- 使用Double-Buffer来实现无闪烁动态折线图
- c#实现golang 的channel
- NIO:与 Buffer 一起使用 Channel
- NIO:与 Buffer 一起使用 Channel
- Linux多线程系列-2-条件变量的使用(线程安全队列的实现)
- 使用 ServerSocketChannel 实现的 File 服务器
- 利用boost::mutex实现线程安全列表模板类,附源码及使用例子
- Winform中使用委托实现跨线程安全调用控件
- Java拷贝文件的例子(使用channel实现)
- 解决使用FluorineFx实现Remoting通讯时 报 "Channel definition, mx.messaging.channels.RTMPChannel, can not be found" 错的问题
- winCE下使用一组buffer实现音频回环
- 使用Double-Buffer来实现无闪烁动态折线图
- 查看buffer pool使用