Pthread:为什么人们要费心使用 pthread_exit? [英] Pthread: Why people bother using pthread_exit?

查看:63
本文介绍了Pthread:为什么人们要费心使用 pthread_exit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当您需要终止具有返回值的线程时,pthread_exit() 完全等于 return.当人们可以使用一致的方式,即返回来完成工作时,为什么 Pthread 定义了这样一个重复的接口?

As far as I understand, pthread_exit() exactly equals to return when you need terminate a thread with a return value. When people can use the consistent way, i.e. return, to do the job why Pthread define such a duplicated interface?

推荐答案

我想到的两个原因:pthread_exit

  1. 允许您从调用堆栈中的任何深度退出线程.

  1. Allows you to exit a thread from any depth in the call stack.

如果 TLS 密钥必须在主线程上调用对于主线程来说,调用它们的自由函数. 还有这里:任何已被推送但尚未弹出的取消清理处理程序都以与推送和执行相反的顺序弹出.毕竟取消清理处理程序已经执行,如果线程有任何线程特定的数据,适当的析构函数将以未指定的顺序调用......对 pthread_exit() 是在第一次调用 main() 的线程以外的线程从用于创建它的启动例程返回时创建的. 函数的返回值作为线程的退出状态."

Must be called on the main thread if the TLS keys for the main thread are to have their free functions called. And here as well: "Any cancellation cleanup handlers that have been pushed and not yet popped are popped in the reverse order that they were pushed and then executed. After all cancellation cleanup handlers have been executed, if the thread has any thread-specific data, appropriate destructor functions will be called in an unspecified order... An implicit call to pthread_exit() is made when a thread other than the thread in which main() was first invoked returns from the start routine that was used to create it. The function's return value serves as the thread's exit status."

这篇关于Pthread:为什么人们要费心使用 pthread_exit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆