电影采集网站建设,wordpress主题 夏令营,自己做的网站怎么被搜索出来,网站建设 考虑Linux线程编程
一、线程核心理论基础
1. 线程是什么#xff1f;
定义#xff1a;线程是进程内的执行单元#xff0c;也被称为“轻量级进程”#xff08;Lightweight Process#xff09;#xff0c;隶属于某个进程#xff0c;共享进程的资源#xff08;代码段、数据段、…Linux线程编程一、线程核心理论基础1. 线程是什么定义线程是进程内的执行单元也被称为“轻量级进程”Lightweight Process隶属于某个进程共享进程的资源代码段、数据段、文件描述符等。核心作用实现并发执行将耗时任务拆分到多个线程并行处理提升程序效率如视频渲染、网络请求并发处理。2. 线程的核心特征特征说明资源分配进程是系统最小资源分配单位线程不单独分配资源共享进程资源执行单位线程是系统最小执行单位CPU调度的基本对象层级关系进程内线程平级默认存在一个“主线程”main函数所在线程资源共享线程间共享进程的全局变量、静态变量、文件描述符等仅栈区8MB独立稳定性线程不稳定一个线程崩溃会导致整个进程退出进程相对稳定互不影响创建开销线程创建仅需开辟独立栈区8MB进程创建需分配3GB虚拟地址空间开销更大并发度线程并发度高于进程同一进程内线程切换无需切换地址空间效率更高3. 线程与进程的核心区别对比维度线程Thread进程Process资源分配共享所属进程资源无独立地址空间独立地址空间资源独立代码段、数据段等创建/切换开销小仅开辟栈区大分配完整地址空间通信方式直接访问共享变量通信简单需通过IPC管道、消息队列等通信复杂稳定性低线程崩溃导致进程退出高进程间相互隔离并发效率高线程切换无需地址空间切换低进程切换开销大4. 线程编程核心流程POSIX标准创建多线程通过pthread_create创建子线程指定线程执行函数线程任务执行子线程在回调函数中完成具体任务资源操作、计算等线程资源回收通过pthread_join阻塞回收或pthread_detach自动回收释放线程资源避免内存泄漏。5. 关键线程函数详解POSIX线程函数库libpthread提供了线程操作的核心接口以下是常用函数说明函数原型功能描述int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)创建子线程-thread输出参数存储新线程ID-attr线程属性默认NULL-start_routine线程回调函数执行入口-arg回调函数参数- 返回值成功0失败返回错误码pthread_t pthread_self(void)获取当前线程ID- 返回值当前线程的IDunsigned long类型打印用%luvoid pthread_exit(void *retval)子线程主动退出-retval线程退出状态返回给主线程int pthread_cancel(pthread_t thread)主线程取消指定子线程-thread目标线程ID- 返回值成功0失败返回错误码int pthread_join(pthread_t thread, void **retval)阻塞回收子线程资源-thread待回收线程ID-retval接收子线程退出状态- 返回值成功0失败返回错误码int pthread_detach(pthread_t thread)设置线程分离属性退出后自动回收资源- 无需主线程调用pthread_join6. 线程查看命令# 查看系统所有线程PID进程IDLWP线程IDCOMM线程名称ps-eLo pid,ppid,lwp,stat,comm# 查看线程详细信息含CPU占用、内存等ps-eLf二、实战代码解析8个核心案例以下结合8个实战代码从基础到进阶逐步掌握线程编程技巧所有代码需链接pthread库编译gcc 文件名.c -o 文件名 -lpthread。案例01创建多线程01pthread.c功能创建2个子线程分别执行不同任务发视频、接收控制#includestdio.h#includepthread.h#includestdlib.h#includeunistd.h#includestring.h// 线程1回调函数发视频void*thread_function(void*arg){while(1){printf(发视频...\n);sleep(1);// 每隔1秒执行一次}returnNULL;}// 线程2回调函数接收控制void*thread_function2(void*arg){while(1){printf(接受控制...\n);sleep(1);}returnNULL;}intmain(){pthread_tthread_id;// 线程1 IDpthread_tthread_id2;// 线程2 ID// 创建线程1pthread_create(thread_id,NULL,thread_function,NULL);// 创建线程2pthread_create(thread_id2,NULL,thread_function2,NULL);// 主线程阻塞避免主线程退出导致子线程终止while(1){sleep(1);}return0;}关键说明pthread_create参数线程ID指针、默认属性NULL、回调函数、回调函数参数NULL主线程需保持运行while(1)否则主线程退出后整个进程终止子线程也会被销毁编译命令gcc 01pthread.c -o 01pthread -lpthread运行结果两个子线程交替输出“发视频…”和“接受控制…”实现并发执行。案例02获取线程ID02pthread_self.c功能通过pthread_self()获取主线程和子线程的ID#includestdio.h#includepthread.h#includeunistd.h#includestdlib.h#includestring.hvoid*th1(void*arg){while(1){// 打印子线程1 ID%lu对应unsigned long类型printf(发视频...tid:%lu\n,pthread_self());sleep(1);}returnNULL;}void*th2(void*arg){while(1){printf(接受控制...tid:%lu\n,pthread_self());sleep(1);}returnNULL;}intmain(){pthread_ttid1,tid2;pthread_create(tid1,NULL,th1,NULL);pthread_create(tid2,NULL,th2,NULL);// 打印主线程IDwhile(1){printf(main tid:%lu\n,pthread_self());sleep(1);}return0;}关键说明pthread_self()无参数返回当前线程的ID类型为pthread_t建议用%lu格式化输出运行结果主线程和两个子线程分别输出各自的ID可通过ps -eLo lwp,comm验证线程是否存在。案例03线程退出03pthread_exit.c功能子线程通过pthread_exit()主动退出主线程运行指定次数后退出#includestdio.h#includepthread.h#includestdlib.h#includeunistd.h#includestring.hvoid*th(void*arg){while(1){printf(sub_th %lu\n,pthread_self());sleep(1);}pthread_exit(NULL);// 子线程主动退出此处因while(1)无法执行到仅作演示}intmain(){pthread_ttid;pthread_create(tid,NULL,th,NULL);inti8;// 主线程运行8秒后退出while(i--){printf(main_th %lu\n,pthread_self());sleep(1);}return0;}关键说明pthread_exit(NULL)子线程主动退出参数为退出状态NULL表示无返回值注意主线程退出后子线程会被强制终止即使子线程有while(1)运行结果主线程输出8次后退出子线程同时终止。案例04取消线程04phread_cancel.c功能主线程通过pthread_cancel()取消子线程#includestdio.h#includestdlib.h#includepthread.h#includeunistd.h#includestring.hvoid*thread_func(void*arg){while(1){printf(subth %lu\n,pthread_self());sleep(1);}}intmain(){pthread_ttid;pthread_create(tid,NULL,thread_func,NULL);inti0;while(1){printf(main th %lu\n,pthread_self());sleep(1);i;if(i2){// 运行2秒后主线程取消子线程pthread_cancel(tid);printf(子线程已取消\n);}}return0;}关键说明pthread_cancel(tid)向指定子线程发送取消请求子线程在“取消点”如sleep、printf等系统调用响应运行结果子线程输出2次后被取消主线程继续运行注意pthread_cancel仅发送请求若子线程无取消点如纯计算循环需手动调用pthread_testcancel()设置取消点。案例05线程资源回收05pthread_jion.c功能主线程通过pthread_join()阻塞等待子线程完成回收资源#includestdio.h#includestdlib.h#includeunistd.h#includestring.h#includepthread.hvoid*th(void*arg){inti5;while(i--){printf(workth,%lu\n,pthread_self());sleep(1);}returnNULL;}intmain(intargc,char**argv){pthread_ttid;pthread_create(tid,NULL,th,NULL);// 阻塞等待子线程tid完成回收其资源pthread_join(tid,NULL);printf(子线程已结束主线程退出\n);return0;}关键说明pthread_join(tid, NULL)主线程阻塞直到子线程tid退出避免子线程成为“僵尸线程”资源未回收第二个参数为NULL表示不关心子线程的退出状态运行结果子线程输出5次后退出主线程打印提示后退出。案例06获取线程返回值06pthread_jionret.c功能子线程动态分配内存并返回数据主线程通过pthread_join()获取返回值并释放内存#includestdio.h#includestdlib.h#includestring.h#includepthread.h#includeunistd.hvoid*th(void*arg){// 动态分配内存子线程栈区数据不能返回会随线程退出释放char*str(char*)malloc(20);strcpy(str,我要灭亡了);returnstr;// 返回动态内存地址}intmain(intargc,char*argv[]){pthread_ttid;pthread_create(tid,NULL,th,NULL);void*retNULL;// 回收子线程获取返回值ret指向子线程分配的内存pthread_join(tid,ret);printf(子线程返回值%s\n,(char*)ret);free(ret);// 释放子线程分配的内存避免内存泄漏return0;}关键说明子线程返回值不能是栈区变量线程退出后栈区释放需用malloc动态分配内存主线程通过pthread_join的第二个参数ret接收返回值使用后需手动free运行结果主线程打印子线程返回的字符串“我要灭亡了”。案例07传递结构体参数07.c功能主线程向子线程传递结构体参数子线程打印并返回结构体地址#includestdio.h#includepthread.h#includestdlib.h#includeunistd.h#includestring.h// 定义结构体存储用户信息typedefstruct{charname[20];intage;charaddress[50];}PER;void*th(void*arg){// 将void*类型转换为结构体指针PER*p(PER*)arg;printf(子线程接收的信息\n);printf(name:%s\n,p-name);printf(age:%d\n,p-age);printf(address:%s\n,p-address);