1.pthread_self
#include <pthread.h>
pthread_t pthread_self(void);
功能:获取当前线程的线程号
参数:
@无
返回值:总是会成功,返回调用线程的线程号(在哪个线程内使用,就返回哪个线程的线程号)
实例:
#include <head.h>
void * pthread(void *args){
pthread_t a = (pthread_t)args;//typedef unsigned long int pthread_t;
printf("子线程:主线程进程号 = %#lx,子线程进程号 = %#lx\n",a,pthread_self());
}
int main(int argc,const char * argv[])
{
pthread_t tid;//typedef unsigned long int pthread_t;
if((errno = pthread_create(&tid,NULL,pthread,(void *)pthread_self()))!=0){
perror("pthread_create error");
exit(-1);
}
printf("主线程:主线程进程号 = %#lx,子线程进程号 = %#lx\n",pthread_self(),tid);
sleep(1);// 不能让进程退出,如果进程退出了,线程没有执行的内存了
return 0;
}
2.pthread_exit
void pthread_exit(void *retval);
功能:退出当前的线程(在线程内如果调用了exit/_exit会让进程退出)
参数:
@retval:返回线程退出时候的状态值
返回值:无
实例:
#include <head.h>
void *pthread(void *args)
{
printf("子线程:11111111\n");
pthread_exit(NULL);//让线程退出
printf("子线程:2222222222\n");//此句话线程退出了 不打印
}
int main(int argc, const char *argv[])
{
pthread_t tid; // typedef unsigned long int pthread_t;
if ((errno = pthread_create(&tid, NULL, pthread, NULL)) != 0)
{
perror("pthread_create error");
exit(-1);
}
sleep(1);//先不让进程退出,如果退出了 线程就没有执行内存了
return 0;
}
3.pthread_join
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
功能:阻塞等待回收子线程资源(结合态的线程)
参数:
@thread:线程号
@retval:接收线程退出时候的状态值
返回值:成功返回0,失败返回错误码
实例:
#include <head.h>
void *pthread(void *args)
{
printf("子线程\n");
static int retval = 12345;
printf("子retval = %p\n",&retval);
sleep(1);
pthread_exit((void *)&retval);//让线程退出
printf("2222222222\n");//此句话线程退出了 不打印
}
int main(int argc, const char *argv[])
{
pthread_t tid; // typedef unsigned long int pthread_t;
if ((errno = pthread_create(&tid, NULL, pthread, NULL)) != 0)
{
perror("pthread_create error");
exit(-1);
}
int *retval;//这个指针会在pthread_join内部指向thread中的reaval地址
pthread_join(tid,(void **)&retval); // 阻塞等待回收子线程的资源
printf("主retval = %p\n",retval);
printf("retval = %d\n",*retval);
return 0;
}
4.pthread_detach
线程的状态分为两种:结合态和分离态
结合态:结合态的线程需要使用到pthread_join来回收资源,线程默认的状态就是结合态
分离态:分离态的线程结束后资源会被自动回收。
#include <pthread.h>
int pthread_detach(pthread_t thread);
功能:标记thread线程为分离态
参数:
@thread:线程号
返回值:成功返回0,失败返回错误码
实例:
#include <head.h>
void *thread(void *args)
{
printf("子进程\n");
pthread_detach(pthread_self()); // 将线程标记为分离态
sleep(3);
pthread_exit(NULL); // 让线程退出
}
int main(int argc, const char *argv[])
{
pthread_t tid;
if ((errno = pthread_create(&tid, NULL, thread, NULL)) != 0)
{
perror("pthread_create error");
exit(-1);
}
//pthread_join(tid,NULL);//不用使用到pthread_join回收
while (1){};
return 0;
}
5.pthread_cancel
#include <pthread.h>
int pthread_cancel(pthread_t thread);
功能:给thread线程发送取消的信号
参数:
@thread:线程号
返回值:成功返回0,失败返回错误码
int pthread_setcancelstate(int state, int *oldstate);
功能:设置线程能够被取消
参数:
@state:
PTHREAD_CANCEL_ENABLE:可以被取消(线程默认设置)
PTHREAD_CANCEL_DISABLE:不可以被取消
@oldstate:返回设置前的状态值
返回值:成功返回0,失败返回非0的错误码
int pthread_setcanceltype(int type, int *oldtype);
功能:设置线程何时被取消
参数:
@type:
PTHREAD_CANCEL_DEFERRED:延时取消,需要找到取消点,才能取消线程
(比如:当系统调用用内核空间返回到用户空间的时候就是一个取消点)
PTHREAD_CANCEL_ASYNCHRONOUS:立即被取消
@oldtype:返回设置前的类型值
返回值:成功返回0,失败返回非0的错误码
实例1:
#include <head.h>
int a;
void *thread1(void *args)
{
printf("子线程1...\n");
while (1)
{
sleep(1);
}
}
void *thread2(void *args)
{
printf("子线程2...\n");
sleep(10);
pthread_cancel((pthread_t)args);
while (1);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
if ((errno = pthread_create(&tid1, NULL, thread1, NULL)) != 0)
{
perror("pthread_create error");
exit(-1);
}
if ((errno = pthread_create(&tid2, NULL, thread2, (void *)tid1)) != 0)
{
perror("pthread_create error");
exit(-1);
}
printf("主线程....\n");
while (1)
;
return 0;
}
主线程号LWP27876(第四列) 线程1 27877 线程2 27878 10秒后查看 线程1被取消
实例2:
#include <head.h>
int a;
void *thread1(void *args)
{
// pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);//线程可以被取消 线程默认设置 与实例1结果相同
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);//线程不可以被取消
printf("子线程1...\n");
while (1)
{
sleep(1);
}
}
void *thread2(void *args)
{
printf("子线程2...\n");
sleep(10);
pthread_cancel((pthread_t)args);
while (1)
{
};
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
if ((errno = pthread_create(&tid1, NULL, thread1, NULL)) != 0)
{
perror("pthread_create error");
exit(-1);
}
if ((errno = pthread_create(&tid2, NULL, thread2, (void *)tid1)) != 0)
{
perror("pthread_create error");
exit(-1);
}
printf("主线程....\n");
while (1)
;
return 0;
}
主线程号LWP32519(第四列) 线程1 32520 线程2 32521 10秒后查看 线程1没有被取消