简体   繁体   English

有没有办法进入Cocoa多线程模式而不创建假的NSThread?

[英]Is there any way to enter Cocoa multithreaded mode without creating fake NSThread?

Apple Threading guide says: Apple线程指南说:

For multithreaded applications, Cocoa frameworks use locks and other forms of internal synchronization to ensure they behave correctly. 对于多线程应用程序,Cocoa框架使用锁和其他形式的内部同步来确保它们的行为正确。 To prevent these locks from degrading performance in the single-threaded case, however, Cocoa does not create them until the application spawns its first new thread using the NSThread class. 但是,为了防止这些锁在单线程情况下降低性能,Cocoa不会创建它们,直到应用程序使用NSThread类生成其第一个新线程。 If you spawn threads using only POSIX thread routines, Cocoa does not receive the notifications it needs to know that your application is now multithreaded. 如果仅使用POSIX线程例程生成线程,Cocoa不会收到它需要知道您的应用程序现在是多线程的通知。 When that happens, operations involving the Cocoa frameworks may destabilize or crash your application. 当发生这种情况时,涉及Cocoa框架的操作可能会使您的应用程序不稳定或崩溃。

To let Cocoa know that you intend to use multiple threads, all you have to do is spawn a single thread using the NSThread class and let that thread immediately exit. 为了让Cocoa知道你打算使用多个线程,你所要做的就是使用NSThread类生成一个线程并让该线程立即退出。 Your thread entry point need not do anything. 你的线程入口点不需要做任何事情。 Just the act of spawning a thread using NSThread is enough to ensure that the locks needed by the Cocoa frameworks are put in place. 只是使用NSThread生成线程的行为足以确保Cocoa框架所需的锁定到位。

In my iOS app, I'm starting several pthreads from C++ code right from the start. 在我的iOS应用程序中,我从一开始就从C ++代码开始几个pthread。 To be sure the app behaves right, according to the doc above, I create a fake NSThread that does nothing. 为了确保应用程序的行为正确,根据上面的文档,我创建了一个无效的假NSThread。 I don't like creating such useless code (usually it's WTF, when you first read it) and I want to avoid doing so. 我不喜欢创建这样无用的代码(通常它是WTF,当你第一次阅读它时)我想避免这样做。 Is there any better way to put my app into multithreaded mode? 有没有更好的方法将我的应用程序置于多线程模式?

If there is, it's not public and might be unstable. 如果有,它不公开,可能不稳定。

If you're hitting a WTF in your code, rename and rejigger things so that it makes sense. 如果您在代码中使用WTF,请重命名并重新调整内容以使其有意义。 Since you need a dummy object with a dummy selector as well, you could just add a throwaway class like CocoaMultithreading and then send it a +beginMultithreading message: 既然你需要一个带有虚拟选择器的虚拟对象,你可以添加一个像CocoaMultithreading这样的一次性类,然后发送一个+beginMultithreading消息:

@interface CocoaMultithreading : NSObject
+ (void)beginMultithreading;
@end

int
main(void) {
    [CocoaMultithreading beginMultithreading];
    /* now do whatever you want */
    return EXIT_SUCCESS;
}

@implementation CocoaMultithreading
+ (void)dummyThread:(id)unused
{
    (void)unused;
}

+ (void)beginMultithreading
{
    [NSThread detachNewThreadSelector:@selector(dummyThread:)
            toTarget:self withObject:nil];
}
@end

That should be explicit enough. 这应该是足够明确的。

ETA: Alexander Staubo points out that, since OS X 10.5/iOS 2.0, you can call the -start method on an NSThread directly, so the very simplest way to flip on Cocoa multithreading would be this: ETA: Alexander Staubo指出,自OS X 10.5 / iOS 2.0以来,您可以直接在NSThread上调用-start方法,因此翻转Cocoa多线程的最简单方法是:

void XXXActivateCocoaMultithreading(void) { [[NSThread new] start]; }

Then, in your main function: 然后,在你的main功能:

XXXActivateCocoaMultithreading();

That is explicit, as well, but far less messy. 这也是明确的,但不那么混乱。 (The XXX is there to remind you to prefix non-static functions. Since static functions often become non-static at some point, prefixing them all from the start is a good move.) XXX用于提醒您为非静态函数添加前缀。由于静态函数在某些时候通常会变为非静态函数,因此从一开始就为它们添加前缀是一个很好的举措。)

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

相关问题 同步此多线程代码(iOS / Cocoa)的正确方法是什么? - What is the correct way to synchronize this multithreaded code (iOS/Cocoa)? 为特定的NSThread创建GCD队列 - Creating a GCD queue for a specific NSThread 有没有办法在 Visual Studio 中以单线程模式调试多线程应用程序? - Is there a way to debug multithreaded applications in singletreaded mode in Visual Studio? 创建多线程音乐播放器 - creating a multithreaded music player 在多线程模式下读取多个文件 - Reading multiple files in multithreaded mode 如何在多线程模式下读取文件? - How to read files in multithreaded mode? 在使用boost的多线程c ++程序中,是否有任何方法可以获取指向当前线程的指针? - In a multithreaded c++ program using boost, is there any way to get a pointer to the current thread? 在多线程环境中启动 std::async function 后,有什么方法可以加速 future.get() function? - Is there any way to speedup the future.get() function after launching the std::async function in a multithreaded environment? 有什么方法可以使Enter / LeaveCriticalSection留下句柄 - Is there any way that Enter/LeaveCriticalSection could leave a handle behind 在多线程程序中创建OpenGL结构? - Creating OpenGL structures in a multithreaded program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM