1

Conceptually I run following Swift pseudo code to regulate UDP sent data bitrate

let sendQueue = DispatchQueue(label: "_send_queue", qos: .userInitiated)
sendQueue.async {
    for data in datas {
        socket.send(data)

        if let timespec = getTimeToSleep() {
            var ts = timespec
            nanosleep(&ts, nil) 
        }
    }
}

In my case time talculated to sleep is usually about 0.001s. In general the real spent in nanosleep() is a couple of percent higher. So far so good - but after some time nanosleep() return after MUCH longer time - easily it's seconds or even a minute. I'm pretty sure I do not request such a long interval. I tried following with no result

  • build the app with release config
  • ran the app outside debugger

What is strange that if this happens while ran from debugger - when I pause and resume the app it runs fine again for a while.

Any clues?

simpleone
  • 360
  • 3
  • 10
  • 1
    You are blocking inside an async block which is an anto pattern – raam86 Jan 16 '18 at 16:16
  • Just made more testing: I even removed `socket.send()` so at the end I only call `nanosleep` in a loop. Same result. – simpleone Jan 16 '18 at 16:18
  • Edit your question to show a minimal example of the behavior you encounter. Your current question seems to ask if there's a bug in `nanosleep` – raam86 Jan 16 '18 at 16:19

1 Answers1

2

Ah, found the reason for this behavior. The macOS app nap is causing this.

Executing

serverActivity = ProcessInfo().beginActivity(options: ProcessInfo.ActivityOptions.userInitiated, reason: "Serving Fast Data")

before I start to serve UDP data makes all stable in time and neat.

simpleone
  • 360
  • 3
  • 10