【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;
}

🎄三

🎄四

🎄五

🎄六、

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

相关推荐
czhc11400756638 分钟前
Linux 76 rsync
linux·运维·python
蓝易云1 小时前
Qt框架中connect()方法的ConnectionType参数使用说明 点击改变文章字体大小
linux·前端·后端
花落已飘2 小时前
多线程 vs 异步
linux·网络·系统架构
PanZonghui2 小时前
Centos项目部署之Nginx部署项目
linux·nginx
码出钞能力3 小时前
linux内核模块的查看
linux·运维·服务器
星辰云-3 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
聽雨2374 小时前
02每日简报20250704
linux·科技·金融·生活·社交电子·娱乐·媒体
Maki Winster4 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek
liulilittle4 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
Maki Winster5 小时前
在 Ubuntu 下配置 oh-my-posh —— 普通用户 + root 各自使用独立主题(共享可执行)
linux·运维·ubuntu