我正在尝试创建一个具有推送和弹出功能的队列结构.
我需要使用10个线程推送和另外10个线程弹出数据,就像我在下面的代码中所做的那样.
问题:1.我需要打印出我有多少推/弹,但我不知道该怎么做.2.无论如何加速我的代码?代码对我来说太慢了.
package main
import (
"runtime"
"time"
)
const (
DATA_SIZE_PER_THREAD = 10000000
)
type Queue struct {
records string
}
func (self Queue) push(record chan interface{}) {
// need push counter
record <- time.Now()
}
func (self Queue) pop(record chan interface{}) {
// need pop counter
<- record
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
//record chan
record := make(chan interface{},1000000)
//finish flag chan
finish := make(chan bool)
queue := new(Queue)
for i:=0; i<10; i++ {
go func() {
for j:=0; j<DATA_SIZE_PER_THREAD; j++ {
queue.push(record)
}
finish<-true
}()
}
for i:=0; i<10; i++ {
go func() {
for j:=0; j<DATA_SIZE_PER_THREAD; j++ {
queue.pop(record)
}
finish<-true
}()
}
for i:=0; i<20; i++ {
<-finish
}
}
Run Code Online (Sandbox Code Playgroud)
jim*_*imt 13
你应该解决一些问题.
队列类型上的方法应该有指针接收器.否则,每个方法调用都将创建当前队列类型的副本,并且对队列字段的任何更改都不会超出方法调用本身.
等待所有例程完成,可以使用a完成sync.WaitGroup.这是它的特别之处.
在队列类型中维护线程安全的推/弹计数器可以通过使用该sync/atomic包来完成.
就速度而言,从你的例子来看,我不太清楚你想要实现的目标.如果你详细说明,可能会出现任何优化.
这是我从您的代码修改的示例:
package main
import (
"log"
"runtime"
"sync"
"sync/atomic"
"time"
)
const SizePerThread = 10000000
type Queue struct {
records string
count int64
}
func (q *Queue) push(record chan interface{}) {
record <- time.Now()
newcount := atomic.AddInt64(&q.count, 1)
log.Printf("Push: %d", newcount)
}
func (q *Queue) pop(record chan interface{}) {
<-record
newcount := atomic.AddInt64(&q.count, -1)
log.Printf("Pop: %d", newcount)
}
func main() {
var wg sync.WaitGroup
runtime.GOMAXPROCS(runtime.NumCPU())
record := make(chan interface{}, 1000000)
queue := new(Queue)
// We are launching 20 goroutines.
// Let the waitgroup know it should wait for as many
// of them to finish.
wg.Add(20)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
for j := 0; j < SizePerThread; j++ {
queue.push(record)
}
}()
go func() {
defer wg.Done()
for j := 0; j < SizePerThread; j++ {
queue.pop(record)
}
}()
}
// Wait for all goroutines to finish.
wg.Wait()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8056 次 |
| 最近记录: |