int pthread_equal(pthread_t tid1, pthread_t tid2);
pthread_t pthread_self(void);
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg);
关于主线程和新线程之间的竞争,简单处理方式是可使主线程进行短暂休眠,确保新线程运行前主线程不会退出。
Linux系统使用clone系统调用创建子进程,来实现pthread_create。
如果进程中任一线程调用了exit, _Exit, _exit,那么整个进程就会终止。
将默认动作是终止进程的信号发送给线程,将终止线程所在整个进程。
void pthread_exit(void *rval_ptr);
进程中其他线程可以通过调用pthread_join,阻塞等待指定线程,直到被指定线程调用pthread_exit、 从启动例程中返回或被取消。
int pthread_join(pthread_t thread, void **rval_ptr);
同一进程环境下,线程可通过调用pthread_cancel函数来取消其他线程。
int pthread_cancel(pthread_t tid);
pthread_cancel并不等待线程终止,仅提出请求。(函数的返回值仅表示请求是否成功,而不表示指定线程是否真实终止?)
void pthread_cleanup_push(void (*rtn)(void *), void *arg);
void pthread_cleanup_pop(int execute);
(3)非零execute参数调用pthread_cleanup_pop;
注意:如果线程由启动例程中返回而终止,则不会调用清理处理程序。
如果execute为0,则清理函数将不被调用;但无论ececute是否为0,pthread_cleanup_pop都将删除上次pthread_cleanup_push所注册的清理处理程序。
6.默认情况下,线程的终止状态将保存至对该线程调用pthread_join。pthread_join可主动使线程进入分离状态。分离状态下线程的底层存储资源可以在线程终止时立即被回收。
联系客服