On Thu, Nov 1, 2012 at 9:34 PM, Robert Sandra wrote:
Hi all,
I am now using multi-goroutines in my program. As far as I know, when I use
the command "go somefunc()" in my code, it will create a new goroutine, but
Go does not guarantee when the new goroutine will start to execute. Is there
any way that I can let the new goroutine start to execute immediately?
Thanks,
Robert
Here are three options that I can think of:
http://play.golang.org/p/-Zg0fg4VElI don't think that option A is guaranteed to begin executing your
goroutine. It does in the example because there is nothing else to do,
but in another application there could be some other goroutine waiting
to run.
Option B may do what you want, but I believe that it could also switch
back to the parent goroutine at the close(ch) call. As a result, it
wouldn't really accomplish anything useful.
Option C seems reasonable, but there could be some nuances with
sync.Mutex that I'm not aware of.
What is the actual problem you are trying to solve? In general, it
shouldn't matter when the new goroutine begins executing. It will run
as needed to satisfy channel operations. Beyond that, it's hard to
predict or control. I think the plan is to introduce preemption into
the scheduler at some point, which will also alter goroutine
execution.
- Max
--