linux 线程函数、结构体

来源:互联网 发布:软件开发流程管理制度 编辑:程序博客网 时间:2024/05/01 00:44

001:

typedef unsigned long intpthread_t;

用途:pthread_t用于声明线程ID。

sizeof (pthread_t) =8;

002:

pthread_join(...)

头文件 : #include <pthread.h>

函数定义: int pthread_join(pthread_t thread, void **retval);

pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果进程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。

thread: 线程标识符,即线程ID,标识唯一线程。

retval: 用户定义的指针,用来存储被等待线程的返回值。

返回值 : 0代表成功。 失败,返回的则是错误号。

003:

pthread_create(...)

#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

若线程创建成功,则返回0。若线程创建失败,则返回出错编号,并且*thread中的内容是未定义的。

第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的起始地址。
最后一个参数是运行函数的参数。

因为pthread并非Linux系统的默认库,而是POSIX线程库。在Linux中将其作为一个库来使用,因此加上 -lpthread(或-pthread)以显式链接该库。函数在执行错误时的错误信息将作为返回值返回,并不修改系统全局变量errno,当然也无法使用perror()打印错误信息。


0 0
原创粉丝点击