简体   繁体   English

在posix线程上创建自动释放池

[英]Create autorelease pool on posix thread

I'm using a GTMLogger functions for formatted logging in my application. 我正在使用GTMLogger函数在应用程序中进行格式化日志记录。 This application creates real-time posix threads (audio packets processing). 该应用程序创建实时posix线程(音频数据包处理)。 Sometimes we need to perform a logging from within these non-Cocoa threads. 有时我们需要从这些非Cocoa线程中执行日志记录。 GTMLogger creates autoreleased objects and I thought to put an @autoreleasepool block on GTMLogger calls in the function, that logs from non-Cocoa threads, but I'm afraid it will affect the performance. GTMLogger创建自动释放的对象,我想在该函数中的GTMLogger调用上放置一个@autoreleasepool块,该块从非Cocoa线程进行日志记录,但恐怕会影响性能。

My question is - can I somehow create the autorelease pool and associate it with the current posix thread, so the pool is created only once per thread and not each time the log function is called? 我的问题是-我可以以某种方式创建自动释放池并将其与当前posix线程相关联,以便每个线程仅创建一次该池,而不是每次调用log函数时都创建该池吗?

Thanks in advance 提前致谢

Under ARC, using @autoreleasepool is quite fast. 在ARC下,使用@autoreleasepool相当快。 If you're concerned about performance in there, I would worry much more about avoiding unnecessary logging calls in the first place than about the autorelease pool. 如果您担心其中的性能,那么与自动释放池相比,我将更加担心首先避免不必要的日志记录调用。

You can't generally create a pool that is associated with "the thread" if the thread is long-lived. 如果线程是长期存在的,则通常无法创建与“线程”关联的池。 You need to to drain the pool periodically, and that won't happen if you just bracket the entire thread main function in an @autoreleasepool{} or anything similar to that. 您需要定期排空池,如果仅将整个线程主函数放在@autoreleasepool{}或类似的括号中,则不会发生这种情况。

好吧,如果您无法控制线程的主要功能,那么我想您最好的办法就是将自动释放池放置在该库“调出”到您控制的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM