• Linux--线程 创建、等待、退出


    Linux上线程开发API概要

    多线程开发的最基本概念主要包含:线程,互斥锁,条件。
      线程 3 种操作:线程的创建,退出,等待。
      互斥锁 4 种操作:创建,销毁,加锁和解锁。
      条件 5 种操作:创建,销毁,触发,广播和等待。
    引用https://www.cnblogs.com/xiehongfeng100/p/4620852.html
    图片引用https://www.cnblogs.com/xiehongfeng100/p/4620852.html

    一、线程的创建、退出及等待

    1、创建
    编译时:gcc 文件 -pthread
    (多线程开发在 Linux 平台上已经有成熟的 pthread 库支持。)

    #include 
    int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
    //指向pthread_t的长整型指针,线程的属性,函数的指针,传参的参数
    // 返回:若成功返回0,否则返回错误编号
    
    • 1
    • 2
    • 3
    • 4

    当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于定制各种不同的线程属性,暂可以把它设置为NULL,以创建默认属性的线程。

    新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。

    2、退出:

    #include 
    int pthread_exit(void *rval_ptr);
    //rval_ptr是一个无类型指针,与传给启动例程的单个参数类似。进程中的其他线程可以通过调用pthread_join函数访问到这个指针。
    
    • 1
    • 2
    • 3

    单个线程可以通过以下三种方式退出,在不终止整个进程的情况下停止它的控制流:
    1)线程只是从启动例程中返回,返回值是线程的退出码。
    2)线程可以被同一进程中的其他线程取消。
    3)线程调用pthread_exit:

    3、等待:
    join 可以接收 exit 退出的返回值

    #include 
    int pthread_join(pthread_t thread, void **rval_ptr);
    // 返回:若成功返回0,否则返回错误编号
    
    • 1
    • 2
    • 3

    4、脱离5、线程ID获取参考资源参考地址

    6、例:

    //线程
    
    #include 
    #include 
    
    void *func1(void *arg)
    {
    	static int ret = 10;
    	
    	printf("ti: %ld thread is create!\n",(unsigned long)pthread_self());
    	printf("ti: param is %d \n",*((int *)arg));
    	
    	//退出
    	pthread_exit((void *)&ret)
    }
    
    int main()
    {
    	int ret;
    	int param = 100;
    	pthread_t t1;
    	
    	int *pret = NULL;
    	
    	//创建线程
    	ret = pthread_create(&t1,NULL,func1,(void *)&param);//调用func1函数
    	if(ret == 0){
    		printf("main: create t1 success! \n");
    	}
    	
    	printf("main: %ld\n",(unsigned long)pthread_self());
    	
    	//等待
    	pthread_join(t1,(void **)&pret);
    	
    	printf("main: t1 quit:%d !\n",*pret);
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    运行结果:

    main: create t1 success!
    main: 139839630968576
    ti: 139839622682368 thread is create!
    ti: param is 100
    main: t1 quit:10 !
    
    • 1
    • 2
    • 3
    • 4
    • 5

    资源参考地址: https://www.cnblogs.com/xiehongfeng100/p/4620852.html

  • 相关阅读:
    AVL双旋转思路分析与图解
    【Mysql系列】04_事务
    Java计算机毕业设计电费管理系统源码+系统+数据库+lw文档
    基于深度学习的端到端语音识别时代
    HTTP协议
    2.3队列
    npm install 出错,‘proxy‘ config is set properly. See: ‘npm help config‘
    奇安信java面试
    十二、SpringBoot文件上传使用及流程分析【文件上传参数解析器】
    shiro与springMVC整合
  • 原文地址:https://blog.csdn.net/weixin_48208102/article/details/132966081