I want to create a background thread on the iPhone that executes some code every 10msec.
But before I get lost in the concurrency programming guide and the threading programming guide of apple again, I wanted to ask whether someone here can help me out.
What I want to do:
- Create a background worker thread
- Trigger the execution of a method every 10msec. (probably by using a NSTimer in that thread?)
- Put as less load on the main application thread as possible
In my understanding subclassing NSThread and writing my own main method in that subclass should do the trick. That way I don’t use an NSTimer for the update intervals but something like:
[NSThread sleepForTimeInterval: 0.01];
Also queuing mechanisms between main thread and worker thread don’t make any sense, as the worker thread should perform the same operation over and over – until stopped.
The question is: How can I configure the thread to use a timer? I can’t see how I can attach a NSTimer to that worker threads Run Loop?
It’s possible to do this using the method you outline, but have you looked at using Grand Central Dispatch? It can make some of this a lot easier:
You can also use a timer dispatch source to do work periodically: