pthread_create()如何工作?

K-R*_*RAN 10 c c++ pthreads

鉴于以下内容:

pthread_t thread;
pthread_create(&thread, NULL, function, NULL);
Run Code Online (Sandbox Code Playgroud)
  • 究竟pthread_create做了thread什么?

  • thread它加入主线程并终止后会发生什么?

  • 如果在thread加入后,您会这样做:

    pthread_create(&thread, NULL, another_function, NULL);
    
    Run Code Online (Sandbox Code Playgroud)

Ste*_*sop 5

pthread_create究竟对线程做了什么?

thread是一个对象,它可以保存一个值来标识一个线程.如果pthread_create成功,则填充标识新创建的线程的值.如果失败,则thread调用后的值未定义.(参考:http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html)

线程加入主线程后终止会发生什么?

对象没有任何反应,但它所持有的值不再引用任何线程(例如,您不能再将它传递给采用a的函数,pthread_t如果您不小心这样做,则可能会ESRCH返回错误).

如果在线程加入后执行此操作,会发生什么:

与之前相同:如果pthread_create成功,则分配一个标识新创建的线程的值.