Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8522025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:59:33+00:00 2026-06-11T06:59:33+00:00

I’m trying to make a queue struct that have push and pop functions. I

  • 0

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:

  1. I need to print out how much I have pushed/popped, but I don’t know how to do that.
  2. 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
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:59:34+00:00Added an answer on June 11, 2026 at 6:59 am

    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. This
      is specifically what it was designed for.

    • Maintaining a thread-safe push/pop counter inside the queue type can be done by
      using the sync/atomic package.

    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:

    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()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.