Linux Pthread函数:多线程编程实战指南
linux pthread函数

首页 2024-12-31 04:42:33



Linux下的多线程编程:pthread函数详解 线程技术自60年代被提出以来,经历了多年的发展和完善,最终在80年代被广泛应用于操作系统中

    现代操作系统,如Windows/NT和Linux,都广泛支持多线程技术

    相较于进程,线程在同一个进程内共享地址空间和大部分数据,具有更高的执行效率和更快速的上下文切换速度

    在Linux系统中,多线程编程遵循POSIX线程接口,简称pthread,提供了一系列丰富的函数用于线程的创建、管理、同步和退出

     一、线程的基本概念和优势 多线程技术通过在一个进程中创建多个线程,实现并发执行

    每个线程都有自己的执行路径,但共享进程的地址空间和资源

    这种机制带来了以下显著优势: 1.提高程序响应速度:通过创建新线程处理耗时操作(如按键响应),不会阻塞主程序的执行,从而提高程序的整体响应速度

     2.改善程序结构:复杂的业务逻辑可以拆分成多个线程处理,使得程序结构更加清晰,便于维护和扩展

     3.更好的资源利用:在SMP(对称多处理)系统中,多个线程可以分配到不同的CPU上运行,充分利用多核处理器的优势

     二、pthread函数详解 Linux下的pthread库提供了一系列用于线程管理的函数,涵盖了线程的创建、退出、同步、属性设置等多个方面

     1.线程的创建:pthread_create() `pthread_create()`函数用于创建一个新线程

    其函数原型如下: c intpthread_create(pthread_t restrict tidp, const pthread_attr_trestrict attr, void (start_rtn)(void ), void restrict arg); -`tidp`:指向线程标识符的指针

     -`attr`:用于设置线程的属性,可以设置为NULL使用默认属性

     -`start_rtn`:新线程执行的函数地址

     -`arg`:传递给新线程函数的参数

     示例代码: c include include include voidthreadFunction(void arg) { printf(Thread started with argument: %ld , (long)arg); return NULL; } intmain(){ pthread_t thread; long arg = 42; int result =pthread_create(&thread, NULL, threadFunction, (void)arg); if(result) { fprintf(stderr, Error creating threadn); return 1; } pthread_join(thread, NULL); return 0; } 2.线程的退出:pthread_exit() `pthread_exit()`函数用于在线程内部显式地退出线程,并返回终止状态

    其函数原型如下: c voidpthread_exit(void value_ptr); -`value_ptr`:指向线程的返回值的指针,可以设置为NULL表示不返回任何值

     示例代码: c include include include voidthreadFunction(void arg) { intvalue_ptr = (int)arg; printf(Thread function executed ); pthread_exit((void)value_ptr); } intmain(){ pthread_t thread; int retVal = 42; pthread_create(&thread, NULL, threadFunction,(void)&retVal); voidexitStatus; pthread_join(thread, &exitStatus); int- threadRetVal = (int)exitStatus; printf(Thread exit status: %dn,threadRetVal); return 0; } 3.线程的取消:pthread_cancel() `pthread_cancel()`函数用于请求取消一个线程的执行

    其函数原型如下: c intpthread_cancel(pthread_t thread); -`thread`:被取消的目标线程的线程ID

     需要注意的是,`pthread_cancel()`函数的行为是异步的,目标线程在达到取消点时才会真正响应取消请求

    因此,取消操作的成功与否取决于目标线程中的取消点和响应策略

     4.线程的等待:pthread_join() `pthread_join()`函数用于阻塞当前线程,直到另一个线程执行结束

    其函数原型如下: c intpthread_join(pthread_t thread, voidthread_return); -`thread`:被等待的线程ID

     -`thread_return`:用于存储被等待线程的返回值

     示例代码已在前面的`pthread_exit()`示例中展示

     5.线程的属性设置:pthread_attr_init()等 pthread库还提供了一系列函数用于设置和获取线程的属性,包括是否脱离、堆栈大小、调度策略等

     -`pthread_attr_init()`:初始化线程属性

     -`pthread_attr_setdetachstate()`:设置线程的脱离状态

     -`pthread_attr_getdetachstate()`:获取线程的脱离状态

     -`pthread_attr_setstacksize()`:设置线程的堆栈大小

     -`pthread_attr_getstacksize()`:获取线程的堆栈大小

     -`pthread_attr_setschedparam()`:设置线程的调度参数

     -`pthread_at

MySQL连接就这么简单!本地远程、编程语言连接方法一网打尽
还在为MySQL日期计算头疼?这份加一天操作指南能解决90%问题
MySQL日志到底在哪里?Linux/Windows/macOS全平台查找方法在此
MySQL数据库管理工具全景评测:从Workbench到DBeaver的技术选型指南
MySQL密码忘了怎么办?这份重置指南能救急,Windows/Linux/Mac都适用
你的MySQL为什么经常卡死?可能是锁表在作怪!快速排查方法在此
MySQL单表卡爆怎么办?从策略到实战,一文掌握「分表」救命技巧
清空MySQL数据表千万别用错!DELETE和TRUNCATE这个区别可能导致重大事故
你的MySQL中文排序一团糟?记住这几点,轻松实现准确拼音排序!
别再混淆Hive和MySQL了!读懂它们的天壤之别,才算摸到大数据的门道