开发者

Speed up NSThread message passing

I'm running 2d animations at 12fps in separate threads(2 to 5).

Each thread display image at the specified time at specified location by "performSelector:withObject:afterDelay"

It works fine for 1 animation, but once I have two or more threads to run 2+ animations at the same time, animation slows down noticeably.

It turns out that NSThread takes much more time than specified(in afterDelay)

performSelector:withObject:afterDelay

when 2+ threads are calling 'performSelector' simultaneously around 12 times per sec per thread.

Wonder if I can configure NSThread to pick up message queued by performSelector faster.

I'm also wondering what is slowing down the message pick-up. maybe thread switching is slow?

Thank you

Below is threadMain code I use.

- (void) myThreadMain
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // Add your sources or timers to the run loop and do any other setup.                                                                                                                                                                                                     
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

    do
    {
        // Start the run loop but return after each source is handled.                                                                                                                                                                                                        
        SInt32    result = CFRunLoopRun开发者_开发知识库InMode(kCFRunLoopDefaultMode, 10, YES);

    }
    while (self.isNeedToExit == false);

    [pool release];

    SYSLOG(LOG_DEBUG, "thread exiting");
}


Well man, the answer is simple. You don't get the multicore functionality through multithreads. It means that only one processor is working with all of your threads. So when you are loading 1 animations and there is only 1 thread, there are no thread switches. When there are 2 ore more animations processor is switching between them. Which leads to much more time execution. I have the same situation with my graphs with animation. Imaging you have normally 1 animation which should run in 1 second and the other animation should normally run in 2 seconds, then when you load both of them simultaneously, the result time would be about 3 second for both.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜