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

🎄三

🎄四

🎄五

🎄六、

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

相关推荐
陌路2025 分钟前
Linux16-进程间的通信--共享内存
linux
运维帮手大橙子30 分钟前
Docker监控系统中添加NodeExporter
linux·运维
是苏浙31 分钟前
零基础入门C语言之操作符详解2
c语言·开发语言
Lzc7741 小时前
Linux网络的应用层协议HTTP
linux·1024程序员节·应用层协议http
susu10830189111 小时前
FAT32/VFAT 文件系统不支持 Linux 文件权限,cp文件总是异常
linux·运维·服务器
絔离1 小时前
Linux下查看系统启动时间、运行时间
linux·运维·服务器
m0_748233641 小时前
单调队列【C/C++】
c语言·c++·算法·1024程序员节
小莞尔1 小时前
【51单片机】【protues仿真】基于51单片机智能温控风扇系统
c语言·单片机·嵌入式硬件·物联网·51单片机·1024程序员节
呆呆小金人2 小时前
Linux:开源时代的隐形基石
linux·1024程序员节
扶尔魔ocy2 小时前
【Linux C/C++开发】epoll模式的开源库及原生socket实现
linux·网络编程·epoll