pthread_create() 如何工作? [英] How does pthread_create() work?
问题描述
鉴于以下内容:
pthread_t thread;
pthread_create(&thread, NULL, function, NULL);
pthread_create
到底对thread
做了什么?What exactly does
pthread_create
do tothread
?thread
加入主线程并终止后会发生什么?What happens to
thread
after it has joined the main thread and terminated?如果在
thread
加入后,你这样做会发生什么:What happens if, after
thread
has joined, you do this:pthread_create(&thread, NULL, another_function, NULL);
推荐答案
pthread_create 到底对线程做了什么?
What exactly does pthread_create do to thread?
thread
是一个对象,它可以保存一个值来标识一个线程.如果pthread_create
成功,它会填充一个值来标识新创建的线程.如果失败,则调用后thread
的值是未定义的.(参考:http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html)thread
is an object, it can hold a value to identify a thread. Ifpthread_create
succeeds, it fills in a value that identifies the newly-created thread. If it fails, then the value ofthread
after the call is undefined. (reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html)线程加入主线程并终止后会发生什么?
What happens to thread after it has joined the main thread and terminated?
对象没有任何反应,但它持有的值不再引用任何线程(例如,您不能再将其传递给采用
pthread_t
的函数,如果您不小心这样做了您可能会返回ESRCH
错误.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 getESRCH
errors back).如果在线程加入后,你这样做会发生什么:
What happens if, after thread has joined, you do this:
与之前相同:如果
pthread_create
成功,则会分配一个值来标识新创建的线程.Same as before: if
pthread_create
succeeds, a value is assigned that identifies the newly-created thread.这篇关于pthread_create() 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!