In our Android app, we have UI component and core C++11 module. A thread is running based on std::chrono::system_clock::time_point
, such as below:
while(this->m_ConditionVariable.wait_until(lock, this->m_Object.to_time_point())
== std::cv_status::no_timeout)
{
// ... handle any notify() or arbitrary sleep breaks
}
Execute(); // <--- not being called consistently
Now, we are testing with 1 minute time_point
. If the app is in use, then the Execute()
is invoked as expected. However, if the app is moved to background or if even the screen is locked, then the Execute()
-s behavior is not consistent.
Sometimes, it may work properly every minute for 15 mins and after that it will be invoked after 2 minutes or 3 minutes or 10 minutes, instead of fixed 1 minute. Using debugs, we checked that, the time_point
supplied is proper.
Suppose if we run the app in debug mode (using Android Studio) then it works fine even in background and screen locked mode.
Does Android have any threading priority for the app running in background?
Update 1: Basically the background thread is collecting location information. I came across below question, which suggests that in Android, when the phone is locked, the thread execution is halted. Am I stuck to that problem?
App seems to stop working when the screen goes to sleep
Update 2: With partial Wake lock, it works fine. But not sure if that's a good solution. If that's the only way, then I would appreciate strategy for how to use it optimally.
Update 3: If I replace wait()
with smaller sleep()
, then it works fine even without any Android wake lock. However we are yet to do regressive testing on it.
sleep()
things works. i.e. if I putsleep(1)
, then it will come out of it after 1 second. Actually, we tried Java Alarm API. At high level, it seems that this API affects to the main GUI thread. C++ thread remained unaffected & still couldn't wake-up. It will be beneficial, if you can put some more info on this API & any relation it has with C++ threads. – Sulfate