当前位置: 首页 > news >正文

自学嵌入式day32,线程

线程基本概念
  • 线程定义:在 Linux 中,线程属于某个进程,是轻量级的执行单元。每个进程默认有一个主线程,线程间是平级关系。
  • 作用:实现并发执行,提高资源利用率和响应速度。
  • 特征
    • 进程是最小资源分配单位,线程是最小执行单位。
    • 线程共享进程资源(如内存空间),但拥有独立的栈区(通常 8MB)。
    • 稳定性较差:若一个线程崩溃,可能导致整个进程崩溃。
    • 创建开销小:线程创建只需在进程空间中开辟新栈区,而进程创建需分配更多资源(如 3GB 空间)。
    • 并发度高:线程并发优于进程,因为切换成本更低。
线程与进程的区别
  • 资源共享:线程共享进程资源(全局变量、堆等),进程资源独立。
  • 稳定性:进程更稳定(独立地址空间),线程不稳定(共享资源)。
  • 创建开销:线程开销小(仅栈区),进程开销大(完整资源分配)。
  • 并发度:线程并发效率更高,适合高并发场景。
线程编程步骤(POSIX)
  1. 创建线程:使用pthread_create函数。
  2. 线程操作:在线程函数中执行任务。
  3. 资源回收:通过pthread_join或设置分离属性回收资源(避免内存泄漏)。
查看线程信息

在 Linux 终端,可使用以下命令查看线程:

ps -eLf # 显示进程和线程信息 ps -elf # 详细列表
线程相关函数详解

下面逐一解释您列出的函数,并提供代码示例。所有示例基于 C 语言,使用 POSIX 线程库。

  1. 获取线程 ID (pthread_t pthread_self(void))

    • 功能:返回当前线程的 ID(pthread_t类型,通常为unsigned long)。
    • 参数:无。
    • 返回值:成功返回线程 ID,失败返回非零错误码。
    • 示例
      #include <stdio.h> #include <pthread.h> void *thread_func(void *arg) { pthread_t tid = pthread_self(); printf("Thread ID: %lu\n", (unsigned long)tid); pthread_exit(NULL); } int main() { pthread_t tid; pthread_create(&tid, NULL, thread_func, NULL); pthread_join(tid, NULL); return 0; }
  2. 线程退出 (void pthread_exit(void *retval))

    • 功能:线程自行退出,可传递退出状态(如错误码或消息)。
    • 参数retval为退出状态指针(可自定义数据类型)。
    • 返回值:无。
    • 示例
      #include <stdio.h> #include <pthread.h> void *thread_func(void *arg) { printf("Thread exiting...\n"); int *status = malloc(sizeof(int)); *status = 42; // 自定义退出状态 pthread_exit(status); } int main() { pthread_t tid; void *retval; pthread_create(&tid, NULL, thread_func, NULL); pthread_join(tid, &retval); printf("Thread exit status: %d\n", *(int *)retval); free(retval); return 0; }
  3. 请求结束线程 (int pthread_cancel(pthread_t thread))

    • 功能:向指定线程发送取消请求(线程需设置可取消状态)。
    • 参数thread为目标线程 ID。
    • 返回值:成功返回 0,失败返回非零错误码。
    • 示例
      #include <stdio.h> #include <pthread.h> #include <unistd.h> void *thread_func(void *arg) { pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); while(1) { printf("Thread running...\n"); sleep(1); } pthread_exit(NULL); } int main() { pthread_t tid; pthread_create(&tid, NULL, thread_func, NULL); sleep(3); // 等待 3 秒后取消线程 pthread_cancel(tid); pthread_join(tid, NULL); printf("Thread canceled.\n"); return 0; }
  4. 线程资源回收 (int pthread_join(pthread_t thread, void **retval))

    • 功能:阻塞等待指定线程结束并回收资源(栈、状态等)。
    • 参数
      • thread:目标线程 ID。
      • retval:接收线程退出状态(需匹配pthread_exit的返回值)。
    • 返回值:成功返回 0,失败返回非零错误码。
    • 示例:见以上pthread_exit示例。
  5. 设置分离属性 (int pthread_detach(pthread_t thread))

    • 功能:设置线程为分离状态,退出后系统自动回收资源(无需pthread_join)。
    • 参数thread为目标线程 ID(通常线程自身调用)。
    • 返回值:成功返回 0,失败返回非零错误码。
    • 示例
      #include <stdio.h> #include <pthread.h> void *thread_func(void *arg) { pthread_detach(pthread_self()); // 设置自身为分离状态 printf("Detached thread running.\n"); pthread_exit(NULL); } int main() { pthread_t tid; pthread_create(&tid, NULL, thread_func, NULL); sleep(1); // 主线程短暂等待 printf("Main thread continues.\n"); return 0; // 分离线程资源由系统回收 }
完整示例:线程创建与回收

以下程序展示创建多个线程、使用共享资源,并回收资源:

#include <stdio.h> #include <pthread.h> #define NUM_THREADS 3 void *worker(void *arg) { int thread_num = *(int *)arg; printf("Thread %d started. ID: %lu\n", thread_num, (unsigned long)pthread_self()); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int thread_args[NUM_THREADS]; // 创建线程 for (int i = 0; i < NUM_THREADS; i++) { thread_args[i] = i; if (pthread_create(&threads[i], NULL, worker, &thread_args[i]) != 0) { perror("pthread_create error"); return 1; } } // 回收线程资源 for (int i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } printf("All threads completed.\n"); return 0; }
注意事项
  • 资源管理:避免内存泄漏,确保回收线程资源(使用pthread_join或设置分离属性)。
  • 线程安全:共享资源时需用互斥锁(如pthread_mutex)防止竞态条件。
  • 错误处理:检查函数返回值,使用perrorstrerror诊断错!
http://www.jsqmd.com/news/101023/

相关文章:

  • 2025年河南工业大学2025新生周赛(8)
  • C语言之最大公约数和最小公倍数问题
  • 设计模式的定义与应用场景 - f
  • 唯一屹立的厂商: Elastic 在 2025 AV-Comparatives 测试中的全面胜出
  • 发现一个可以真的一句话操作电脑的AI工具,居然还是开源的!
  • 金运环球:金银走势分化待非农破局,早盘关注关键技术位防守
  • 书籍是进步的阶梯,职场人自我提升必看的书籍推荐
  • Coze工作流下载:AI如何自动化你的开发流程
  • 基于VirtualBox使用ISO创建Linux镜像
  • 汽车免拆诊断案例|2023 款智己LS7车仪表偶尔提示前向防碰撞辅助功能不可用
  • LobeChat零售业商品推荐引擎整合方案
  • 汽车免拆诊断案例 | 本田Insight混合动力系统冷却风扇故障深度解析
  • 什么是静态住宅ip,跨境电商为什么要用静态住宅ip
  • 为什么map函数比for循环快?性能对比实测
  • O(log N) 对数计算
  • 【震惊!】护士注册选错机构?这3点必须知道!
  • 使用Docker快速启动LobeChat镜像的5种方式
  • Detect It Easy原型开发:快速验证你的想法
  • 蓝牙定位追踪技术:从技术原理、核心优势详解(一)
  • 剧本杀剧情设计:LobeChat构造悬疑故事情节
  • 如何在Android中使用StateFlow和MutableStateFlow?
  • 用于氧化石墨烯的多模态表征与激光还原图案化的共聚焦显微技术
  • 盲盒一番赏小程序开发:解锁千亿级潮玩市场的技术密码
  • Dify默认端口修改全攻略(含API配置)
  • 室内蓝牙定位追踪技术:从典型场景到技术局限性与优化方向详解(二)
  • 场馆预约小程序开发:解锁 “预约经济” 的高效解决方案
  • ES6模板字符串深度解析:原理、应用与Tagged Template高级用法
  • Docker 整体架构
  • 应用材料 0195-02529
  • Nano Banana Pro:设计师的竞争对手还是强有力的助手?