线程创建(UNIX环境高级编程笔记)

  新增的线程可以通过pthread_create函数创建。

#include <pthread.h>
int pthread_create(pthread_t *restrict tipd,
         const pthread_attr_t *restrict attr,
         void *(*start_rtn)(void *), void *restrict arg);
              返回值:若成功,返回0;否则,返回错误编号。

  当pthread_create成功返回时,新创建线程的线程ID会被设置成tidp指向的内存单元。attr参数用来定制各种不同的线程属性,置空时为默认属性。
  新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。如果需要向start_rtn函数传递的参数有一个以上。那么需要把这些参数放到一个结构体中,然后把这个结构体中的地址作为arg参数传入。
  注意,pthread函数在调用失败时通常会返回错误码,不会设置errno。每个线程都提供errno的副本,这只是为了与使用errno的现有函数兼容。

猜你喜欢

转载自blog.csdn.net/The_perfect_world/article/details/89420947