简体   繁体   English

pthread_create()如何工作?

[英]How does pthread_create() work?

Given the following: 给定以下内容:

pthread_t thread;
pthread_create(&thread, NULL, function, NULL);
  • What exactly does pthread_create do to thread ? pthread_createthread究竟做了什么?

  • What happens to thread after it has joined the main thread and terminated? 发生什么thread它已加入主线程和终止之后?

  • What happens if, after thread has joined, you do this: 如果在thread加入后执行此操作,会发生什么情况:

     pthread_create(&thread, NULL, another_function, NULL); 

What exactly does pthread_create do to thread? pthread_create对线程有什么作用?

thread is an object, it can hold a value to identify a thread. thread是一个对象,它可以保存一个值以标识线程。 If pthread_create succeeds, it fills in a value that identifies the newly-created thread. 如果pthread_create成功,则它将填充一个标识新创建线程的值。 If it fails, then the value of thread after the call is undefined. 如果失败,则调用后的thread值不确定。 (reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html ) (参考: http : //pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html

What happens to thread after it has joined the main thread and terminated? 加入主线程并终止后,线程会如何处理?

Nothing happens to the object, but the value it holds no longer refers to any thread (so for example you can no longer pass it to functions that take a pthread_t , and if you accidentally do then you might get ESRCH errors back). 该对象没有任何反应,但是它拥有的值不再引用任何线程(例如,您不能再将其传递给采用pthread_t函数,并且如果您不小心这样做了,则可能会返回ESRCH错误)。

What happens if, after thread has joined, you do this: 如果在线程加入后执行此操作,会发生什么情况:

Same as before: if pthread_create succeeds, a value is assigned that identifies the newly-created thread. 与以前相同:如果pthread_create成功,则会分配一个值,该值标识新创建的线程。

pthread_create will create a thread using OS calls. pthread_create将使用OS调用创建线程。 The wonderful things about abstraction is that you don't really need to care what's happening below. 关于抽象的奇妙之处在于,您实际上不需要关心下面发生的事情。 It will set the variable thread equal to an identifier that can be used to reference that thread. 它将变量线程设置为等于可用于引用该线程的标识符。 For example, if you have multiple threads and want to cancel one of them just call 例如,如果您有多个线程并想要取消其中一个线程,只需调用

pthread_cancel(thread) pthread_cancel(线程)

using the right pthread_t identifier to specify the thread you're interested in. 使用正确的pthread_t标识符指定您感兴趣的线程。

What happens to thread after it has joined the main thread and terminated? 加入主线程并终止后,线程会如何处理?

Before the thread terminates the var thread serves as a key/index to get at or identify a thread. 在线程终止之前,var线程用作获取或标识线程的键/索引。 After the thread terminates the value that the key/index pointed to no longer has to be valid. 线程终止后,键/索引指向的值不再有效。 You could keep it around and try to reuse it, but that's almost certain to cause errors. 您可以保留它并尝试重用它,但是几乎可以确定会导致错误。

What happens if, after thread has joined, you do this: 如果在线程加入后执行此操作,会发生什么情况:

pthread_create(&thread, NULL, another_function, NULL);

No problem, since you give it a reference to thread the value of thread will be set to an identifier for the new thread that was just made. 没问题,因为您给它提供了对线程的引用,所以线程的值将被设置为刚刚创建的新线程的标识符。 I suspect its possible that it could be the same as before, but I wouldn't count on it. 我怀疑它可能和以前一样,但我不会指望它。

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

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