线程函数

来源:互联网 发布:火狐 谷歌 知乎 编辑:程序博客网 时间:2024/06/05 13:09

创建线程:

    pthread_create(pthread_t *tid,const pthread_attr_t *attr, void *(*__start_routine) (void *), void *__restrict __arg )

参数1: 新建线ID 

参数2:新建线程属性,可能为NULL 

参数3:线程执行的函数指针,其函数原型为void * name(void *arg);

参数4:线程执行函数的参

 

退出线程:

    pthread_exit(void *rval_p);

参数1:返回值;

 

等待线程结束:

    pthread_join(pthread_t tid,void **rval_ptr);

参数1:等等的线程ID 

参数2:指身线程返回值的指针

 

 

取消线程:

   pthread_cancel(pthread_t tid);

   相当于线程自己调用pthread_exit()

 

线程退出清理函数绑定:

    pthread_cleanup_push(void (*rtn((void *),void *arg)

参数1:清理函数指针

参数2:传给清楚函数的参数

  调用清理函数的三种情况:

    1、调用pthread_exit()

    2、调用pthread_cancel()

    3、调用pthread_cleanup_pop()并传入非0参数时;

 

线程退出清理函娄执行:

pthread_cleanup_pop(int style)

style为0时,不执行清理函数,但将栈顶的中清除出栈;

如果设置多个清理函数时,以先进后出的原则顺序执行

原创粉丝点击