加快 NSThread 消息传递速度

发布于 2024-10-15 22:58:34 字数 1449 浏览 5 评论 0原文

我在单独的线程(2 到 5)中以 12fps 运行 2d 动画。
每个线程通过“performSelector:withObject:afterDelay”在指定时间在指定位置显示图像
它对于 1 个动画效果很好,但是一旦我有两个或更多线程同时运行 2 个以上动画,动画就会明显减慢。

事实证明,

performSelector:withObject:afterDelay

当 2 个以上线程以每个线程每秒约 12 次的速度同时调用“performSelector”时,NSThread 花费的时间比指定的时间(在 afterDelay 中)要多得多。

不知道我是否可以配置 NSThread 来更快地获取由 PerformSelector 排队的消息。
我还想知道是什么减慢了消息接收速度。也许线程切换很慢?

谢谢

下面是我使用的threadMain代码。

- (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 = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, YES);

    }
    while (self.isNeedToExit == false);

    [pool release];

    SYSLOG(LOG_DEBUG, "thread exiting");
}

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 = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, YES);

    }
    while (self.isNeedToExit == false);

    [pool release];

    SYSLOG(LOG_DEBUG, "thread exiting");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-10-22 22:58:34

好吧,伙计,答案很简单。您无法通过多线程获得多核功能。这意味着只有一个处理器正在处理所有线程。因此,当您加载 1 个动画并且只有 1 个线程时,就没有线程切换。当有 2 个或以上动画时,处理器会在它们之间切换。这会导致更多的执行时间。我的动画图表也有同样的情况。
想象一下,您通常有 1 个动画应在 1 秒内运行,而另一个动画通常应在 2 秒内运行,那么当您同时加载这两个动画时,两者的结果时间约为 3 秒。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文