I'm not quite sure how to avoid this, but I'm pretty sure that I know the
cause. Read up on Grand Central Dispatch and Centralized Task Scheduling (
https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/DiscretionaryTasks.html).
It's been a while since I've written OS X code, but I used to do it quite
heavily (I'm one of the authors of the mac version of Google Earth).

OS X tries very hard to keep the CPU's in their lowest power state, the the
scheduler has all sorts of heuristics for when an application needs to be
woken up. By default, it tries very hard to keep the applications scheduled
as little as possible, and you're supposed to provide hints to the system
about the urgency of the work you're doing.

Now, here's where I'm rusty, but I believe that at very low nice levels,
you can bypass this behavior. Try this:

sudo nice -1 /path/to/your/application

If your timers start working correctly, that's probably the reason.

Generally, when you interact with Cocoa system libraries, there's all sorts
of interaction with GCD and CTS to keep your app running in an
efficiency/performance sweet spot, but when you call out to another
runtime, like Go's, you bypass all those hooks.


On Wed, Sep 4, 2019 at 1:20 AM Tor Langballe <torlangba...@gmail.com> wrote:

> I'm running a mac cocoa event loop, and calling a go ticker:
>
> void StartApp() {
>     [NSAutoreleasePool new];
>     [NSApplication sharedApplication];
>     [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
>     [NSApp run];
> }
>
> func test() {
>  last := time.Now()
>  ticker := time.NewTicker(time.Second)
>  for range ticker.C {
>      t := time.Now()
>      diff := t.Sub(last)
>      if diff > time.Second*2 {
>          fmt.Println("Tick slow:", diff)
>      }
>      last = t
>   }
> }
>
>
> func main() {
>  go test()
>  C.StartApp()
> }
>
> currenly just running the go program from the terminal, though I got
> similar results packaging it into an app.
>
> I soon get very long periods between ticker fires:
>
> Tick slow: 3.501350114s
> Tick slow: 5.551485377s
>
> This seems to only happen with
>
> [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
>
> NSApplicationActivationPolicyProhibited and
> NSApplicationActivationPolicyAccessory don't cause this.
>
> I assume it's the event loop in [NSApp run] that is causing this, but
> don't know how to start making a mac appliction with goroutines and tickers
> that don't get blocked.
>
> Does anybody know how to avoid this and why it is happening?
>
> tor
>
>
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/62325cef-7441-4a7c-abf9-ff58454ff5f4%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/62325cef-7441-4a7c-abf9-ff58454ff5f4%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LvAMukbgVPV_ARiOsm7X7zpUcdnOA-BLQ9%3DEdX3yeZYFg%40mail.gmail.com.

Reply via email to