【Linux C | 多线程编程】线程的退出

😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀
🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C++、数据结构、音视频🍭
⏰发布时间⏰:

本文未经允许,不得转发!!!

目录


🎄一、概述

本文主要介绍线程退出的几种方式,以及各种方式之间的区别

✨1.1

✨1.2

🎄二、线程会终止, 进程不会终止 的三种方式

下面的三种方法中,线程会终止,但是进程不会终止(如果线程不是进程组里的最后一个线程的话):

  • 创建线程时的 start_routine 函数执行了return, 并且返回指定值。
  • 线程调用 pthread_exit
  • 其他线程调用了 pthread_cancel 函数取消了该线程。

例子一:线程函数使用 return 终止线程

c 复制代码
// 06_pthread_return.c
// gcc 06_pthread_return.c -l pthread
#include <stdio.h>
#include <pthread.h>
void *func(void *arg)
{
	int *parg = arg;
	printf("this thread arg is %d, my threadID is %lx \n", *parg, (unsigned long)pthread_self());
	return NULL;
}
int main()
{
	int arg=10;
	pthread_t threadId;
	pthread_create(&threadId, NULL, func, &arg);
	return 0;
}

例子二:线程函数使用 pthread_exit 终止线程

c 复制代码
// 06_pthread_exit.c
// gcc 06_pthread_exit.c -l pthread
#include <stdio.h>
#include <pthread.h>
void *func(void *arg)
{
	int *parg = arg;
	printf("this thread arg is %d, my threadID is %lx \n", *parg, (unsigned long)pthread_self());
	pthread_exit(NULL);
}

int main()
{
	int arg=10;
	pthread_t threadId;
	pthread_create(&threadId, NULL, func, &arg);
	return 0;
}

🎄三

🎄四

🎄五

🎄六、

如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁

相关推荐
码农编程录2 小时前
【notes11】并发与竞争
linux
mobai72 小时前
Ubuntu环境上安装NTP服务
linux·运维·ubuntu
郝学胜-神的一滴2 小时前
Linux Socket编程核心:深入解析sockaddr数据结构族
linux·服务器·c语言·网络·数据结构·c++·架构
云深麋鹿3 小时前
五.排序笔记
c语言·数据结构·算法·排序算法
海上Bruce9 小时前
C primer plus (第六版)第十二章 编程练习第3题
c语言
小天源9 小时前
linux漏洞一键扫描
linux·运维·服务器·漏洞扫描
SundayBear9 小时前
零基础入门MQTT协议
c语言·单片机
我能坚持多久10 小时前
D16—C语言内功之数据在内存中的存储
c语言·开发语言
码农水水11 小时前
中国邮政Java面试被问:容器镜像的多阶段构建和优化
java·linux·开发语言·数据库·mysql·面试·php
福楠11 小时前
C++ STL | map、multimap
c语言·开发语言·数据结构·c++·算法