l6-d5 线程的创建和参数传递

本文介绍了Linux线程的基本概念,如进程与线程的区别,以及使用pthread库创建线程的方法。涵盖了线程同步机制(如信号量和互斥锁)、线程ID获取、参数传递和处理段错误的示例。最后给出一个创建10个子线程的编程作业。
摘要由CSDN通过智能技术生成

一、线程的基本特点

进程

        进程有独立的地址空间

        Linux为每个进程创建task_struct

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

线程

        进程在切换时系统开销大

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

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

        Linux不区分进程、线程

线程特点

        通常线程指的是共享相同地址

        空间的多个任务

        使用多线程的好处:大大提高了任务切换的效率、避免了额外的TLB & cache的刷新的刷新

线程共享资源

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

线程私有资源

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

二、pthread库函数创建线程

Linux线程库:

pthread线程库中提供了如下基本操作

        创建线程

        回收线程

        结束线程

同步和互斥机制

        信号量

        互斥锁

线程创建

#include  <pthread.h>

int  pthread_create(pthread_t *thread, const pthread_attr_t *attr,

                                                        void *(*routine)(void *), void *arg);

        成功返回0,失败时返回错误码

        thread 线程对象

        attr 线程属性,NULL代表默认属性

        routine 线程执行的函数

        arg 传递给routine的参数 ,参数是void * ,注意传递参数格式

代码演示:

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

void *testThread(void *arg){
    printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
   // return NULL;

    pthread_exit(NULL);
    printf("after pthread exit\n");
}
int main(){
    pthread_t tid;
    int ret;
    
    ret = pthread_create(&tid,NULL,testThread,NULL);

    printf("This is main thread,tid=%lu\n",tid);    
    sleep(1);
}

注意

编译时候需链接 -lpthread

主进程的退出,它创建的线程也会退出

线程创建需要时间,如果主进程马上退出,那线程不能得到执行

三、线程ID的获取

1.线程结束

pthread_exit

#include  <pthread.h>

        void  pthread_exit(void *retval);

        结束当前线程

        retval可被其他线程通过pthread_join获取

        线程私有资源被释放

2.线程查看tid函数

#include <pthread.h>

        pthread_t pthread_self(void);        //线程获得自己的ID

四、线程的参数传递

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

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

void *testThread(void *arg){
    printf("This is a thread test,pid=%d,tid=%lu\n",
                            getpid(),pthread_self());//进程id,线程id
    printf("input arg=%d\n",*(int *)arg);//地址传递参数,强制类型转化
    pthread_exit(NULL);        //终止线程
    printf("after pthread exit\n");
}
int main(){
    pthread_t tid;
    int ret;
    int arg = 5;
   
    ret = pthread_create(&tid,NULL,testThread,(void *)arg);    //arg变量的地址作为参数传递

    printf("This is main thread,tid=%lu\n",tid);    
    sleep(1);
}

五、段错误的原因及处理方法

#include <pthread.h>
#include <stdio.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);
    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,testThread,(void *)i);
     
//        sleep(1);   //休眠,等待线程
        printf("This is main thread,tid=%lu\n",tid[i]);    
    }
    while(1){
        sleep(1);
    }
}

六、作业

使用pthread_create实现 10 个子线程,并且让每个子线程打印自己的线程号

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

void * testThread(void *arg){
	printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
	printf("This is %d thread\n",(int)arg);
	pthread_exit(NULL);
}

int main(){
	pthread_t tid[10];
	int ret;
	int i;
	int arg;
	for(i = 0; i <= 10; i++){
		ret = pthread_create(&tid[i],NULL,testThread,(void *)i);
		printf("This is main thread,tid=%lu\n",tid[i]);
	}
	sleep(1);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值