I’m trying to make a queue struct that have push and pop functions.
I need to use 10 threads push and another 10 threads pop data, just like I did in the code below.
Questions:
- I need to print out how much I have pushed/popped, but I don’t know how to do that.
- Is there anyway to speed up my code? The code is too slow for me.
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
}
}
There are a few things you should fix.
The methods on the Queue type should have pointer receivers. Otherwise, every method
call will create a copy of the current queue type and any changes to queue fields will
not persist beyond the method call itself.
Waiting for all routines to finish, can be done using a
sync.WaitGroup. Thisis specifically what it was designed for.
Maintaining a thread-safe push/pop counter inside the queue type can be done by
using the
sync/atomicpackage.As far as speed goes, from your example, I am not quite sure what you are trying to achieve. Any optimizations might come up if you elaborate on that a little.
Here is an example I modified from your code: