0

I am using CPP and i want to implement timeout mechanism.
Form Example, function called "foo()" which will just trigger one function to execute after some timeout and it will return.

Why i need this?

I have one Android application which uses one CPP library (using JNI interface) in that lib there are some functions which takes time to process. I can't call these functions in separate thread (either from java or CPP as lib limitations) and because of that i get ANR in android application.

So what i will do, i will call one function in CPP layer using JNI which will start one timer and will return so that UI thread in android will not give ANR and when this timer is timed out it will call that time taking function and when processing is done it will inform the java layer.

How should i implement this?

User7723337
  • 11,857
  • 27
  • 101
  • 182
  • This question should help: http://stackoverflow.com/questions/879896/c-how-to-implement-a-timeout-for-an-arbitrary-function-call – Dennis Aug 17 '12 at 09:33
  • is there any way we can implement timer in cpp, which will come one function on timeout. – User7723337 Aug 17 '12 at 10:02
  • Why you "can't call these functions in separate thread" ? You cannot do without threads. If it is a time consuming, un-interruptable process, you either execute it in the main thread (and get ANR) or in another thread. If your execution fires upon timer, it will either already be a separate thread (in which the timer was ticking), or the timer will signal some thread. It can also signal the main thread, but then you have no solution. – Pavel Zdenek Aug 17 '12 at 14:15

1 Answers1

0

In your native C++ code you can use POSIX timers and any other POSIX functions as well:

http://www.kernel.org/doc/man-pages/online/pages/man2/timer_create.2.html

To facilitate many standard tasks (timers, threads, sockets etc) you may want to consider using boost library. It requires a little study though. There's no official Android port of boost. But there are instructions how to build boost such as in this book http://www.wowebook.be/book/android-ndk-beginners-guide/#

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158