为什么 pthread_exit 使用 void*?
Why does pthread_exit use void*?
我最近开始使用 posix 线程,标准中参数类型的选择让我很好奇。我无法回答 pthread_exit use void* instead of int for the returning status of the thread? (the same as exit 为什么会这样的问题。
我看到的唯一优点是它可以让程序员定义他们想要的状态(例如 return 指向复杂结构的指针),但我怀疑它是否被广泛使用。
似乎在大多数情况下,由于需要转换,这种选择的开销更大。
这不仅仅是状态,它是线程的 return 值。使用指针允许线程 return 指向动态分配的数组或结构的指针。
您不能真正将它与 exit()
参数进行比较,因为它用于向操作系统发送状态。这是有意非常简单的,以允许与许多操作系统的可移植性。
The only advantage I see is that it lets the programmers define the status how they want (e.g. return a pointer to a complex structure), but I doubt it is widely used like this.
确实是这个原因。而且它可能没有被广泛使用(例如,您可以通过其他方式传递值,例如传递给线程函数的指针、带同步的全局变量等)。但是如果你有一个像 void pthread_exit(int);
这样的它,它就会失去指向 return 指针的能力。所以void pthread_exit(void*);
是比较灵活的设计。
It seems that in most cases this choice has more overhead because of necessary casting.
在大多数情况下,根本不使用它,因为常见的方法是 return 什么都不用,即 pthread_exit(NULL);
。因此,只有当 returning 指针(指向结构等)时才重要,在这些情况下,不需要转换为 void *
,因为任何指针类型都可以转换为 void *
而无需显式演员。
我最近开始使用 posix 线程,标准中参数类型的选择让我很好奇。我无法回答 pthread_exit use void* instead of int for the returning status of the thread? (the same as exit 为什么会这样的问题。
我看到的唯一优点是它可以让程序员定义他们想要的状态(例如 return 指向复杂结构的指针),但我怀疑它是否被广泛使用。
似乎在大多数情况下,由于需要转换,这种选择的开销更大。
这不仅仅是状态,它是线程的 return 值。使用指针允许线程 return 指向动态分配的数组或结构的指针。
您不能真正将它与 exit()
参数进行比较,因为它用于向操作系统发送状态。这是有意非常简单的,以允许与许多操作系统的可移植性。
The only advantage I see is that it lets the programmers define the status how they want (e.g. return a pointer to a complex structure), but I doubt it is widely used like this.
确实是这个原因。而且它可能没有被广泛使用(例如,您可以通过其他方式传递值,例如传递给线程函数的指针、带同步的全局变量等)。但是如果你有一个像 void pthread_exit(int);
这样的它,它就会失去指向 return 指针的能力。所以void pthread_exit(void*);
是比较灵活的设计。
It seems that in most cases this choice has more overhead because of necessary casting.
在大多数情况下,根本不使用它,因为常见的方法是 return 什么都不用,即 pthread_exit(NULL);
。因此,只有当 returning 指针(指向结构等)时才重要,在这些情况下,不需要转换为 void *
,因为任何指针类型都可以转换为 void *
而无需显式演员。