How to control go routine numbers in function call

122 views
Skip to first unread message

Joseph Wang

unread,
Jun 7, 2019, 9:28:01 PM6/7/19
to golan...@googlegroups.com
Hi everyone 

I have a simple question about go routines.

So I have a function like this

Func1() {

  for n := range array {
        go fun2()
  }
}

So as I can not make sure array has a fix size, then I cannot control go routines number. Does any one have some elegant ways that I can limit this function1 create fix number go routines?

Best

Joseph

Joseph Wang

unread,
Jun 7, 2019, 9:31:16 PM6/7/19
to golan...@googlegroups.com
I mean at same time only 4 go routines run and then creat another 4 etc.

Michael Jones

unread,
Jun 7, 2019, 9:40:39 PM6/7/19
to Joseph Wang, golan...@googlegroups.com
Here’s a helper...

You can do it using several other mechanisms as well.  

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABp8xEnDcrNHhFO-DPrOOmXAy%2B%3DA04QVcgA%2B5E2DABn-B6dCpQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael T. Jones
michae...@gmail.com

Krzysztof Kowalczyk

unread,
Jun 8, 2019, 2:25:44 AM6/8/19
to golang-nuts

Jesper Louis Andersen

unread,
Jun 12, 2019, 7:14:29 AM6/12/19
to Krzysztof Kowalczyk, golang-nuts
And if you prefer a talk, there is one by Bryan C. Mills,


which describes common concurrency patterns and how to apply them. In your case, a buffered channel can work as a (counting) semaphore. The invariant is: the number of tokens in the channel is the number of currently active workers. If the channel is full and you block, all worker slots are occupied. As workers complete, they remove a token from the channel, making room for another worker.

Finalization is important. You end by _filling_ the semaphore with tokens. These tokens have no workers associated with them, but are "virtual dummy sentinels" in a sense. Once the channel is full of these sentinel tokens, all the real workers must have exited, so you can go on[0]. You can also see this as vacant seats in a restaurant, say. As the restaurant wants to close, it starts filling up the seats with bulbasaurs. Once all seats have bulbasaurs in them, the restaurant can close.

[0] The section is at https://youtu.be/5zXAHh5tJqQ?t=1902 but the whole talk is definitely worth it.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
J.
Reply all
Reply to author
Forward
0 new messages