Thread gets killed when app is sent to background

My app has some native c/c++ library linked. These libraries are creating many threads using pthread. One of the thread is getting killed automatically when I sent my app to the background. I found that the thread is not reponsive.

My app does some activity in the background such as Bluetooth communication to get data from some devices.

Please let me know if anyone has any idea about this.

It’s very unlikely that the thread is getting “killed”. In general, if iOS kills your app in the background, it kills the entire process, not just one thread.

I suspect that the issue here is that your app is getting suspended in the background. That suspension affects all threads running inside your app.

There are ways to allow your app to run in the background but they all have significant limits in order to preserve standby battery life. To learn more about this in general, read the Background Execution section of the App Programming Guide for iOS.

What sort of Bluetooth are you doing? Bluetooth LE, via Core Bluetooth? Or ‘classic’ Bluetooth, to an MFi device via External Accessory framework? Both of these support background execution, but the exact mechanisms differ.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for your reply.


We are using the Core Bluetooth in our app. This particular C thread is used to communicate with Bluetooth thread which intern communicate with Core Bluetooth with few respons. There are other threads also which are also communicating with Bluetooth thread that are working fine in Background.

This issue is not always seen. This is seen 3 out of 10 times. Mostly reproducible in iPhone 6 with iOS version > 8.2.


Thanks.

When Core Bluetooth resumes you in the background, are you holding an assertion to prevent yourself from being suspended while you do this Core Bluetooth request/response dance? If not, it’s well feasible you could be suspended in the middle of it, which generally doesn’t end well.

The above mentioned assertions come in the form of:

  • a UIApplication background task, or

  • an NSProcessInfo activity

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Thread gets killed when app is sent to background
 
 
Q