直白解读linux下pthread的简单操作

本文详细介绍了Linux系统中pthread线程的创建、删除、等待和控制,包括pthread_create、pthread_exit、pthread_join和pthread_detach等函数的使用,强调了多线程编程时需要注意的事项,如主线程应避免干扰其他线程执行,并在编译时需要链接-lpthread库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程:他是轻量级的进程,他有系统的库函数提供,linux中的man 2中的函数,属于系统调用;线程的创建是在进程之中,在linux中用task_struct来描述一个线程,(进程和线程都参与统一的调度),一个进程可以有多个线程,他们可以共享相同地址空间的多个任务。

一个进程中的多个线程共享以下资源:

1:代码段/指令

2:静态数据(全局变量,静态变量)

3:进程中打开的文件描述符

4:信号处理函数

5:用户id,组id

6:当前工作目录

一个进程中每个线程私有的资源:

1:线程号tid

2:寄存器

3:栈/堆(局部变量)

4:执行状态和属性

5:错误码(error)

----------------------------------------创建线程--------------------------------------

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);
Compile and link with -pthread.编译链接的时候需要-pthread
创建一个新的线程
thread :出参,保存新创建的线程的id
attr :新创建的线程的属性,如果是NULL 表示默认/缺省的属性
start_routine :函数指针,新的线程调用的函数
arg :函数start_routine的参数

---------------------------------删除线程/线程退出--------------------------------
void pthread_exit(void *retval);
终止线程
retval :线程退出时返回值的地址

----控制线程,等待一个线程结束(以阻塞的方式等待线程结束)----
1.int pthread_join(pthread_t thread, void **retval);
等待一个线程的结束
thread :要等待的线程
retval :指向线程的返回值的地址

2.int pthread_detach(pthread_t thread);
分离线程
将id号是thread的线程进行分离
3.pthread_t pthread_self(void);
获取线程的id号

-----------------------------------------------------------------------------------------

注意:在进行多线程编程时,一般主线程初始化/创建其他的线程后,不会做任何操作,直接调用pthread_join等待线程结束,因为主线程有操作的话,可能因为操作失误而关闭进程,这样就会影响其他的线程操作

-----------------------------------------------------------------------------------------

注意:gcc编译时要在结尾加 -l pthread链接

eg:1

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
char message[32]="hello world";

int main()
{
        pthread_t t1;   
        void *thread_function(void *);
        void *thread_result;
        int err;

        err = pthread_create(&t1,NULL,thread_function,(void *)message);
        if(err != 0)
        {
                perror("pthread_create");
                exit(1);
        }
        err = pthread_join(t1,&thread_result);
        if(err != 0)
        {
                perror("pthread_join");
                exit(2);
        }
        printf("%s\n", message);
        printf("%s\n", (char *)thread_result);

        return 0;
}

void *thread_function(void *m)
{
    char *message = (char *)m;
    printf("%s\n",message);
    sleep(2);
    strcpy(message,"marked by thread");
    pthread_exit("thank you");
}


eg:2

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

int main()
{
        pthread_t th;
        int i;
        void *func(void *);

        pthread_create(&th,NULL,(void *)func,NULL);
//      pthread_join(th,NULL);      //父线程阻塞等待
//      pthread_detach(th);         //与父进程脱离(最好写在子线程里)父线程继续执行
                                    //如果父线程销毁,那么子线程会受影响,应为共用一个地址空间。
        for(i=0;i<3;i++)
        {
                printf("====%d\n",i);
                sleep(1);
        }
        return 0;
}

void *func(void *m)                     
{
        int i;
        pthread_detach(pthread_self());  //
        printf("child thread  join\n");
        for(i=0;i<6;i++)
        {
                sleep(1);
                printf("child thread\n");
        }
        printf("child thread  exit\n");

        return NULL;
}


基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目),个人经导师指导并认可通过的高分设计项目,评审分98分,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!主要针对计算机相关专业的正在做大作业、毕业设计的学生和需要项目实战练习的学习者,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基于JavaWeb的学生信息管理系统课程设计源码+数据库+文档报告(99分项目)基
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值