gpt4 book ai didi

objective-c - 命令行 Objective-C 程序如何创建其主 NSThread 线程?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:26 26 4
gpt4 key购买 nike

我想知道 objective-c 程序本身是否是 NSThread 对象。一个非常简单的例子:

#import <Foundation/Foundation.h>
#import <stdio.h>


int main()
{
printf("Is this a main thread? %s\n", [NSThread isMainThread] ? "yes" : "no");
printf("This is the main thread object %p\n", [NSThread mainThread]);

return(0);
}

运行它,它给出了:

Is this a main thread? yes
This is the main thread object 0x1928430

当然在这个程序中我没有创建我的“主线程对象”所以我怀疑 objective-c 运行时从我的 main() 函数为我创建了一个 NSThread 对象。但我不知道这种魔法是如何发生的。有人会告诉我 __libc_start_main 和我的 main() 函数之间发生了什么吗? NSThread 类如何知道这是一个主线程?

我用以下代码编译了上面的代码:

clang  -Werror -g -v -I/usr/local/include -fconstant-string-class=NSConstantString -fno-objc-arc -lobjc -lgnustep-base mycode.m -o mycode

在 CentOS linux 6.5 上。

最佳答案

在阅读更多代码之后,我想回答我自己的问题以供将来引用。在夏天,在调用 [NSThread isMainThread] 之前,没有创建 NSThread。在 main() 函数中和之前没有发生什么特别的事情。 NSThread 是由 GSCurrentThread() 的副作用创建的。

基于“gnustep-base-1.24.0”的代码示例。来自源文件“NSThread.m”:

+ (BOOL) isMainThread
{
return (GSCurrentThread() == defaultThread ? YES : NO);
}

它调用 GSCurrentThread() 返回该线程的当前 NSThread 对象。

inline NSThread*
GSCurrentThread(void)
{
NSThread *thr = pthread_getspecific(thread_object_key);
if (nil == thr)
{
GSRegisterCurrentThread();
thr = pthread_getspecific(thread_object_key);
if ((nil == defaultThread) && IS_MAIN_PTHREAD)
{
defaultThread = [thr retain];
}
}
assert(nil != thr && "No main thread");
return thr;
}

thread_object_key是对应线程的静态变量。它未初始化,因此 NSThread 对象 thr 将为 nil 并调用 GSRegisterCurrentThread()

BOOL
GSRegisterCurrentThread (void)
{
return [NSThread _createThreadForCurrentPthread];
}

此函数调用 NSThread 类方法 _createThreadForCurrentPthread

+ (BOOL) _createThreadForCurrentPthread
{
NSThread *t = pthread_getspecific(thread_object_key);

if (t == nil)
{
t = [self new];
t->_active = YES;
[[NSGarbageCollector defaultCollector] disableCollectorForPointer: t];
pthread_setspecific(thread_object_key, t);
GS_CONSUMED(t);
return YES;
}
return NO;
}

再一次,NSThread 对象 t 将为 nil 并且一个新的 NSHead 对象将被分配并由代码 t = [self new]。此 NSThread 对象将被分配一个 pthread 键,该键将存储在静态变量 thread_object_key 中。

最后我们返回到函数GSCurrentThread():

  if (nil == thr)
{
GSRegisterCurrentThread();
// Returned from here
thr = pthread_getspecific(thread_object_key);
if ((nil == defaultThread) && IS_MAIN_PTHREAD)
{
defaultThread = [thr retain];
}
}

pthread_getspecific() 将再次调用,但这次变量 thread_object_key 与来自 _createThreadForCurrentPthread 的 NSThread 对象相关联。如果 defaultThread 没有设置,它将被设置为我们新创建的 NSThread 对象,thr

在堆栈的末尾 [NSThread isMainThread] 将返回 YES 因为 defaultThread 拥有由 创建的 NSThread 对象GSCurrentThread()。从现在开始,进程本身可以通过 thread_object_key 被视为一个 NSThread 对象,它是每个进程/线程的静态变量。这就是 main 函数的 NSThread 对象的创建方式。

关于objective-c - 命令行 Objective-C 程序如何创建其主 NSThread 线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32376466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com