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

  • SEARCH
  • Home
  • 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 8117425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:09:59+00:00 2026-06-06T04:09:59+00:00

I made a simple clock signal using goroutines: func clockloop(ch chan byte) { count

  • 0

I made a simple clock signal using goroutines:

func clockloop(ch chan byte) {
    count := 0
    for {
        time.Sleep(FRAMELEN)
        count++
    innerfor:
        for count {
            select {
                case ch <- 1:
                    count--
                default:
                    break innerfor
            }
        }
    }
}

func MakeClock() chan byte {
    clock := make(chan byte)
    go clockloop(clock)
    return clock
}

I know this clock is imprecise but that is irrelevant. What I want to know is what is the best way to stop this goroutine?

Ideally, I would have liked it to stop as soon as any listeners to the channel go out of scope but I do know how to do that (much like a garbage collector but for goroutines). I have come up with two ways to stop it manually: (1) with a global stop variable that all such goroutines would check every loop and (2) with a stop channel. The disadvantage of each of these is I have to manually stop the goroutines.

  • 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-06T04:10:01+00:00Added an answer on June 6, 2026 at 4:10 am

    A stop channel is a good way to do it. A goroutine must stop itself. There is no way to externally kill one.

    For reference there is this thread on Go-Nuts where Ian says essentially this and Russ adds, “Killing individual goroutines is a very unstable thing to do:
    it’s impossible to know what locks or other resources those
    goroutines had that still needed to be cleaned up for the
    program to continue running smoothly.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK, I have made simple rectangular using OpenGL, and it looks pretty simple glNormal3f(0.0f,
On Windows, testing different OSes is made simple using VMs. Is there a simple
Made a simple app which using a timer, counts the number of mouse clicks
I have made a very simple Pie Graph generator using Javascript & SVG graphics.
I'm still new to PHP/Javascript.I've made a simple Clock program in Javascript successfully. Now
I have made a clock to show current time with an ajax timer in
I have a menu made with HTML, using a simple UL with lots of
I made a simple gallery, using this script to show the images: $rs =
I made a simple game yesterday in javascript using coffeescript. The bulk of the
I made a very simple dropdown using a <div> (parent), <span> (current selection) and

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.