L5进程、线程和进程间通信--线程的创建和回收(day3)

目录

一、线程的概念

1、进程

2、线程

3、线程特点

4、线程共享资源和线程私有资源

5、Linux线程库

二、线程创建 – pthread_create

编译错误分析:

三、线程结束– pthread_exit

四、线程查看tid  函数

注意事项:

 五、线程间参数传递(重点)

第一种方式:通过地址传递

第二种方式:值传递

创建多线程:

六、线程的回收

使用线程的分离:

线程回收的实际效果:

作业:


一、线程的概念

1、进程

进程有独立的地址空间

Linux为每个进程创建task_struct

每个进程都参与内核调度,互不影响

2、线程

进程在切换时系统开销大

很多操作系统引入了轻量级进程LWP

同一进程中的线程共享相同地址空间

Linux不区分进程、线程

3、线程特点

通常线程指的是共享相同地址
    空间的多个任务
使用多线程的好处
大大提高了任务切换的效率
避免了额外的TLB & cache的刷新

4、线程共享资源和线程私有资源

一个进程中的多个线程共享以下资源:
可执行的指令
静态数据
进程中打开的文件描述符
当前工作目录
用户ID
用户组ID

每个线程私有的资源包括:
线程ID (TID)
PC(程序计数器)和相关寄存器
堆栈
错误号 (errno)
优先级
执行状态和属性

5、Linux线程库

Linux内核里并没有实现线程的功能,它是通过调用库来实现的。

pthread线程库中提供了如下基本操作:
  创建线程
  回收线程
  结束线程
同步和互斥机制:
  信号量
  互斥锁

二、线程创建 – pthread_create

 #include  <pthread.h>
 int  pthread_create(pthread_t *thread, const  pthread_attr_t *attr, void *(*routine)(void *), void *arg);

pthread_t *thread是一个数字的指针(pthread_t就是一个数字),pthread_attr_t *attr是线程的结构体

 成功返回0,失败时返回错误码
 thread 线程对象
 attr 线程属性,NULL代表默认属性
 routine 线程执行的函数
 arg 传递给routine的参数 ,参数是void * ,注意传递参数格式,
 

编译错误分析:

1. 
createP_t.c:14:36: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
     ret = pthread_create(&tid,NULL,testThread,NULL);
                                    ^
In file included from createP_t.c:1:0:
/usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int * (*)(char *)’

意义:表示pthread_create参数3的定义和实际代码不符合,期望的是void * (*)(void *) ,实际的代码是int * (*)(char *)
解决方法:改为pthread_create(&tid,NULL,(void*)testThread,NULL);

2.
createP_t.c:(.text+0x4b):对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status   --------这个链接错误,
表示pthread_create这个函数没有实现 
解决方法:编译时候加 -lpthread

三、线程结束– pthread_exit

 #include  <pthread.h>
 void  pthread_exit(void *retval);
       
 结束当前线程
 retval可被其他线程通过pthread_join获取
 线程私有资源被释放

四、线程查看tid  函数

pthread_t  pthread_self(void)   查看自己的TID
#include <pthread.h>
pthread_t pthread_self(void);

注意事项:

1. 主进程的退出,它创建的线程也会退出。
线程创建需要时间,如果主进程马上退出,那线程不能得到执行

获取线程的id
通过pthread_create函数的第一个参数;通过在线程里面调用pthread_self函数

createP_t.c:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

int *testThread(char *arg){
	printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());//调用函数线程内查看
	//return NULL;
	pthread_exit(NULL);//退出线程(与return NULL类似,建议使用本函数,有清理线程的作用)
	printf("after pthread exit\n");
}


int main()
{
	pthread_t tid;
	int ret;
	ret = pthread_create(&tid,NULL,(void*)testThread,NULL);
	printf("This is main thread,tid = %lu\n",tid);//通过获取creat时的第一个参数在主线程查看
	sleep(1);


}

结果:

 五、线程间参数传递(重点)

pthread_create(pthread_t *thread, const  pthread_attr_t *attr, void *(*routine)(void *), void *arg);
 最后一个参数
 

编译错误:
createP_t.c:8:34: warning: dereferencing ‘void *’ pointer
     printf("input arg=%d\n",(int)*arg);
                                  ^
createP_t.c:8:5: error: invalid use of void expression
     printf("input arg=%d\n",(int)*arg);
错误原因是void *类型指针不能直接用*取值(*arg),因为编译不知道数据类型。
解决方法:转换为指定的指针类型后再用*取值  比如:*(int *)arg

第一种方式:通过地址传递

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

void *testThread(void *arg){//这里的arg变成了(void*)型指针
	printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());//调用函数线程内查看
	//return NULL;
	printf("input arg=%d\n",*(int*)arg);//(void*)型指针无法直接取值,需要给一个准确的类型
	pthread_exit(NULL);//退出线程(与return NULL类似,建议使用本函数,有清理线程的作用)
	printf("after pthread exit\n");
}


int main()
{
	pthread_t tid;
	int ret;
	int arg = 5;//这里arg是整型
	ret = pthread_create(&tid,NULL,(void*)testThread,(void*)&arg);
	printf("This is main thread,tid = %lu\n",tid);//通过获取creat时的第一个参数在主线程查看
	sleep(1);


}

结果: 

第二种方式:值传递

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

void *testThread(void *arg){
	printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());//调用函数线程内查看
	//return NULL;
	//printf("input arg=%d\n",arg);//直接用%d打印地址,会告警(%p打印会加0x前缀,但不会告警)
	printf("input arg=%d\n",(int)arg);//把指针类型直接转化为整型(不会报错),本来5是地址这里直接变成整型
	pthread_exit(NULL);//退出线程(与return NULL类似,建议使用本函数,有清理线程的作用)
	printf("after pthread exit\n");
}


int main()
{
	pthread_t tid;
	int ret;
	int arg = 5;//这里arg是整型
	ret = pthread_create(&tid,NULL,(void*)testThread,(void*)arg);//整型直接转化为(void*)型指针,5就是地址
	printf("This is main thread,tid = %lu\n",tid);//通过获取creat时的第一个参数在主线程查看
	sleep(1);


}

结果:

注意:1.    通过地址传递参数,注意类型的转换
2.    值传递,这时候编译器会告警,需要程序员自己保证数据长度正确(int和指针长度都是4,所以才能能运行)

创建多线程:

mthread_t.c:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

void *testThread(void *arg){
	printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
	//return NULL;
	//printf("This is %d thread\n",*(int*)arg);//地址传递
	printf("This is %d thread\n",(int)arg);//值传递
	pthread_exit(NULL);
	printf("after pthread exit\n");
}


int main()
{
	pthread_t tid[5];
	int ret;
	int arg = 5;
	int i;
	for(i=0;i<5;i++){//创建多线程
    //ret = pthread_create(&tid[i],NULL,(void*)testThread,(void*)&i);
	ret = pthread_create(&tid[i],NULL,(void*)testThread,(void*)i);
	printf("This is main thread,tid = %lu\n",tid[i]);
	//sleep(1);//不加此函数,for循环里i都到5执行完了,上面子线程可能才能取到i的值
	
	}

	sleep(1);


}

1、不加sleep(1)的地址传递:(有问题)

2、加sleep(1)的地址传递(效率低)

 3、值传递

 运行错误:

*** stack smashing detected ***: ./mthread_t terminated
已放弃 (核心已转储)

原因:栈被破坏了(数组越界)(本来ret = pthread_create(&tid[i],NULL,(void*)testThread,(void*)i);是   ret = pthread_create(&tid[5],NULL,(void*)testThread,(void*)i);)

六、线程的回收

 #include  <pthread.h>
 int  pthread_join(pthread_t thread, void **retval);
   对于一个默认属性的线程 A 来说,线程占用的资源并不会因为执行结束而得到释放 
 成功返回0,失败时返回错误码
 thread 要回收的线程对象
 调用线程阻塞直到thread结束
 *retval 接收线程thread的返回值
 

注意:pthread_join 是阻塞函数,如果回收的线程没有结束,则一直等待

编译错误:
pjoin.c:13:5: error: unknown type name ‘pthead_t’
     pthead_t tid;
错误类型:未知的类型pthead_t  
错误可能:1拼写错误,2对应的头文件没有包含

pjoin.c:18:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘void *’ [-Wformat=]
     printf("thread ret=%s\n",retv);
错误类型:参数不匹配,期望的是char * ,但参数retv是void *
解决:在参数前面加强制类型转换(char*)retv

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *func(void *arg){
    printf("This is child thread\n");
    sleep(5);
    pthread_exit("thread return");

}


int main(){
    pthread_t tid[5];
    void *retv;
    int i;
    for(i=0;i<5;i++){
        pthread_create(&tid[i],NULL,func,NULL);
    }
    for(i=0;i<5;i++){
        pthread_join(tid[i],&retv);
        printf("thread ret=%s\n",(char*)retv);
    }
   // while(1){    
        sleep(1);
    //} 

}

结果:5秒后全部回收

 

使用线程的分离:

两种方式:
1 使用pthread_detach
2 创建线程时候设置为分离属性
  pthread_attr_t attr; 
  pthread_attr_init(&attr);       //初始化这个属性
  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);    //再设置(属性,分离状态)

pdatt.c:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *func(void *arg){
    printf("This is child thread\n");
    sleep(5);
    pthread_exit("thread return");

}


int main(){
    pthread_t tid[5];
    void *retv;
    int i;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

    for(i=0;i<5;i++){
        pthread_create(&tid[i],&attr,func,NULL);
       // pthread_detach(tid);
    }
    
    while(1){    
        sleep(1);
    } 

}

结果:

线程回收的实际效果:

pjoin.c:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *func(void *arg){
    printf("This is child thread\n");
    sleep(25);
    pthread_exit("thread return");

}


int main(){
    pthread_t tid[100];
    void *retv;
    int i;
    for(i=0;i<100;i++){
        pthread_create(&tid[i],NULL,func,NULL);
    }
    for(i=0;i<100;i++){
        pthread_join(tid[i],&retv);
        printf("thread ret=%s\n",(char*)retv);
    }
    while(1){    
        sleep(1);
    } 

}

结果:

25秒后线程结束打印 thread ret=thread return

25秒前:

25秒后:内存明显减小,说明线程被回收 

 如果没有 pthread_join(tid[i],&retv);    线程不会回收,内存不减反升

pdetach_t.c:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *func(void *arg){
    pthread_detach(pthread_self());
    printf("This is child thread\n");
    sleep(25);
    pthread_exit("thread return");

}


int main(){
    pthread_t tid[100];
    void *retv;
    int i;
    for(i=0;i<100;i++){
        pthread_create(&tid[i],NULL,func,NULL);
       // pthread_detach(tid);
    }
    
    while(1){    
        sleep(1);
    } 

}

结果同上: 

 25秒后:

padtt.c:也是一样的效果。

作业:

编写一个多线程程序,实现线程的回收

参考pjoin.c:     pdetach_t.c:     padtt.c: 


版权声明:本文为qq_48328693原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。