响应式网站wordpresswordpress首页评论

张小明 2026/3/12 15:04:13
响应式网站wordpress,wordpress首页评论,wordpress头像多说,灵山县建设局网站线程#xff08;pthread#xff09;知识点整理1. 线程概念与特点线程 vs 进程特征进程线程资源分配最小资源分配单位最小执行单位资源共享私有资源空间共享进程资源#xff0c;部分私有通信方式IPC#xff08;复杂#xff09;直接通信#xff08;简单#xff09;创建开销…线程pthread知识点整理1. 线程概念与特点线程 vs 进程特征进程线程资源分配最小资源分配单位最小执行单位资源共享私有资源空间共享进程资源部分私有通信方式IPC复杂直接通信简单创建开销大小轻量级进程稳定性高相对较低效率相对较低高节省30%资源线程优点资源共享共享进程内的全局变量、堆区、文件描述符等高效创建销毁开销小上下文切换快并发性好适合I/O密集型任务简化通信直接共享内存无需复杂IPC线程缺点稳定性差一个线程崩溃可能影响整个进程调试复杂gdb调试需要特殊命令缺乏保护线程间没有内存保护2. POSIX线程编程框架三部曲创建线程 → 线程执行 → 资源回收编译与头文件#include pthread.h // 线程头文件 gcc -g -pthread source.c // 编译命令-lpthread永久设置编译别名# 编辑 ~/.bashrc alias gccgcc -g -pthread # 生效配置 source ~/.bashrc3. 线程创建与管理3.1 创建线程int pthread_create( pthread_t *thread, // 线程ID出参 const pthread_attr_t *attr, // 线程属性NULL为默认 void *(*start_routine)(void *), // 线程回调函数 void *arg // 回调函数参数 );重要特性一次只能创建一个线程主线程退出所有子线程也退出线程ID是CPU维护的唯一标识多个线程可执行同一回调函数3.2 获取线程IDpthread_t pthread_self(void); // 获取当前线程ID3.3 查看线程信息命令ps -eLf # 查看所有线程 ps -eLo pid,ppid,lwp,stat,comm # 查看LWP轻量级进程 pstree # 查看线程树结构4. 线程退出与回收4.1 线程退出方式// 1. 自行退出自杀 void pthread_exit(void *retval); // retval: 退出状态 // 2. 强制退出他杀 int pthread_cancel(pthread_t thread);4.2 线程资源回收int pthread_join(pthread_t thread, void **retval);回收策略有限时间结束 →pthread_join阻塞等待可能休眠阻塞 → 超时后强制回收长期运行 → 不回收可能导致资源泄漏4.3 分离属性// 方法1设置属性 pthread_attr_t attr; pthread_attr_init(attr); pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED); pthread_create(tid, attr, func, NULL); pthread_attr_destroy(attr); // 方法2直接分离 int pthread_detach(pthread_t thread);5. 线程参数传递5.1 传递整型参数// 主线程 int value 100; pthread_create(tid, NULL, func, (void *)(long)value); // 子线程 void *func(void *arg) { int val (int)(long)arg; // 使用val return NULL; }5.2 传递字符串参数// 栈区字符数组危险线程结束后可能失效 char str[] hello; pthread_create(tid, NULL, func, str); // 堆区字符串安全 char *str malloc(128); strcpy(str, hello); pthread_create(tid, NULL, func, str); // 线程结束后需要free5.3 传递结构体参数typedef struct { int id; char name[32]; float score; } Student; // 主线程 Student stu {1, Tom, 90.5}; pthread_create(tid, NULL, func, stu); // 子线程 void *func(void *arg) { Student *stu (Student *)arg; printf(ID:%d, Name:%s, Score:%.1f\n, stu-id, stu-name, stu-score); return NULL; }5.4 计算器示例typedef struct { float a; float b; char op; // - * / float result; } Calculator; void *calc_func(void *arg) { Calculator *calc (Calculator *)arg; switch(calc-op) { case : calc-result calc-a calc-b; break; case -: calc-result calc-a - calc-b; break; case *: calc-result calc-a * calc-b; break; case /: if(calc-b ! 0) calc-result calc-a / calc-b; else calc-result 0; break; } return NULL; }6. 线程返回值返回值传递原理pthread_exit(void *retval)→ 返回地址pthread_join(tid, void **retval)→ 接收地址有效返回地址类型全局变量可直接访问意义不大静态变量生命周期长堆区变量推荐可控制生命周期// 示例返回堆区字符串 void *thread_func(void *arg) { char *result malloc(128); strcpy(result, Thread completed successfully); pthread_exit(result); } int main() { pthread_t tid; char *retval; pthread_create(tid, NULL, thread_func, NULL); pthread_join(tid, (void **)retval); printf(Thread returned: %s\n, retval); free(retval); // 必须释放 return 0; }7. 线程清理函数void cleanup_func(void *arg) { printf(Cleanup: %s\n, (char *)arg); } void *thread_func(void *arg) { // 注册清理函数 pthread_cleanup_push(cleanup_func, Resource cleanup); // 线程工作代码 // do something... // 执行清理函数参数为0则不执行 pthread_cleanup_pop(1); // 1:执行, 0:不执行 return NULL; }8. 线程互斥Mutex互斥锁框架// 1. 定义互斥锁 pthread_mutex_t mutex; // 2. 初始化 pthread_mutex_init(mutex, NULL); // 3. 加锁 pthread_mutex_lock(mutex); // 临界区代码 pthread_mutex_unlock(mutex); // 4. 解锁 // 5. 销毁 pthread_mutex_destroy(mutex);非阻塞加锁int pthread_mutex_trylock(pthread_mutex_t *mutex); // 成功: 0, 失败: EBUSY锁已被占用互斥锁示例共享缓冲区#include stdio.h #include pthread.h #include string.h #include unistd.h char buffer[1024]; pthread_mutex_t mutex; void *writer_thread(void *arg) { char *msg (char *)arg; for(int i 0; i 5; i) { pthread_mutex_lock(mutex); strcpy(buffer, msg); printf(%s wrote: %s\n, msg, buffer); pthread_mutex_unlock(mutex); usleep(100000); // 100ms } return NULL; } int main() { pthread_t tid1, tid2; pthread_mutex_init(mutex, NULL); pthread_create(tid1, NULL, writer_thread, Thread1); pthread_create(tid2, NULL, writer_thread, Thread2); pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_mutex_destroy(mutex); return 0; }9. 线程同步信号量信号量框架#include semaphore.h // 1. 定义信号量 sem_t sem; // 2. 初始化二值信号量 sem_init(sem, 0, 1); // 参数信号量pshared(0线程), 初始值 // 3. P操作申请资源 sem_wait(sem); // 临界区 sem_post(sem); // 4. V操作释放资源 // 5. 销毁 sem_destroy(sem);同步示例生产者-消费者#include stdio.h #include pthread.h #include semaphore.h #include string.h char buffer[256]; sem_t sem_empty; // 缓冲区空信号量 sem_t sem_full; // 缓冲区满信号量 void *input_thread(void *arg) { while(1) { sem_wait(sem_empty); // 等待缓冲区空 printf(Enter message: ); fgets(buffer, sizeof(buffer), stdin); buffer[strlen(buffer)-1] \0; // 去掉换行符 if(strcmp(buffer, quit) 0) { sem_post(sem_full); break; } sem_post(sem_full); // 通知处理线程 } return NULL; } void *process_thread(void *arg) { while(1) { sem_wait(sem_full); // 等待数据 if(strcmp(buffer, quit) 0) { sem_post(sem_empty); break; } printf(Processed: Length%lu\n, strlen(buffer)); sem_post(sem_empty); // 通知可继续输入 } return NULL; } int main() { pthread_t tid1, tid2; sem_init(sem_empty, 0, 1); // 初始缓冲区为空 sem_init(sem_full, 0, 0); // 初始无数据 pthread_create(tid1, NULL, input_thread, NULL); pthread_create(tid2, NULL, process_thread, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); sem_destroy(sem_empty); sem_destroy(sem_full); return 0; }10. 综合示例火车票售票系统#include stdio.h #include pthread.h #include unistd.h #include stdlib.h #define TICKET_COUNT 100 int tickets TICKET_COUNT; pthread_mutex_t mutex; int window1_count 0; int window2_count 0; void *sell_tickets(void *arg) { char *window_name (char *)arg; while(1) { pthread_mutex_lock(mutex); if(tickets 0) { printf(%s 卖出车票 %d\n, window_name, TICKET_COUNT - tickets 1); tickets--; if(strcmp(window_name, 窗口1) 0) window1_count; else window2_count; pthread_mutex_unlock(mutex); usleep(100000); // 模拟卖票时间 } else { pthread_mutex_unlock(mutex); break; } } return NULL; } int main() { pthread_t tid1, tid2; pthread_mutex_init(mutex, NULL); pthread_create(tid1, NULL, sell_tickets, 窗口1); pthread_create(tid2, NULL, sell_tickets, 窗口2); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf(\n售票统计:\n); printf(窗口1卖出: %d张\n, window1_count); printf(窗口2卖出: %d张\n, window2_count); printf(总共卖出: %d张\n, TICKET_COUNT - tickets); pthread_mutex_destroy(mutex); return 0; }11. 线程控制函数对比进程操作线程操作功能fork()pthread_create()创建getpid()pthread_self()获取IDexit()pthread_exit()退出wait()pthread_join()等待回收kill()pthread_cancel()强制终止atexit()pthread_cleanup_push/pop()清理函数12. 死锁与预防死锁产生条件互斥条件资源只能被一个线程使用请求与保持线程持有资源并请求新资源不可剥夺资源只能由持有者释放循环等待线程间形成等待环预防策略按顺序申请资源所有线程按相同顺序申请锁使用超时机制pthread_mutex_trylock 超时重试避免嵌套锁尽量减少锁的嵌套层次使用资源层级为资源分配优先级避免死锁示例// 按固定顺序获取锁 void safe_operation(pthread_mutex_t *lock1, pthread_mutex_t *lock2) { // 总是先获取lock1再获取lock2 pthread_mutex_lock(lock1); pthread_mutex_lock(lock2); // 临界区操作 pthread_mutex_unlock(lock2); pthread_mutex_unlock(lock1); }13. 线程调度与让出CPUpthread_yield(); // 主动让出CPU建议使用 usleep(1000); // 休眠方式让出CPU14. 综合练习餐厅管理系统#include stdio.h #include pthread.h #include semaphore.h #include string.h #include stdlib.h #include unistd.h #define TABLE_COUNT 3 typedef struct { char name[100]; int total_num; int call_num; pthread_mutex_t lock; } Restaurant; Restaurant rest; sem_t tables; // 餐桌信号量 void *customer(void *arg) { int id *(int *)arg; printf(顾客%d [%s] 正在用餐...\n, id, rest.name); sleep(rand() % 3 1); // 用餐时间 printf(顾客%d [%s] 离开\n, id, rest.name); sem_post(tables); // 释放餐桌 free(arg); return NULL; } void *waiter(void *arg) { while(1) { pthread_mutex_lock(rest.lock); if(rest.call_num rest.total_num) { sem_wait(tables); // 等待空桌 int *id malloc(sizeof(int)); *id rest.call_num; pthread_t tid; pthread_create(tid, NULL, customer, id); pthread_detach(tid); printf(服务员叫号: %d [%s]\n, rest.call_num, rest.name); } else { pthread_mutex_unlock(rest.lock); break; } pthread_mutex_unlock(rest.lock); sleep(1); } return NULL; } int main() { pthread_t waiter_tid; // 初始化 sem_init(tables, 0, TABLE_COUNT); pthread_mutex_init(rest.lock, NULL); rest.total_num 0; rest.call_num 0; // 输入顾客信息 printf(请输入顾客姓名: ); fgets(rest.name, sizeof(rest.name), stdin); rest.name[strlen(rest.name)-1] \0; printf(请输入顾客总数: ); scanf(%d, rest.total_num); getchar(); // 清除缓冲区 // 启动服务员线程 pthread_create(waiter_tid, NULL, waiter, NULL); pthread_join(waiter_tid, NULL); // 清理 sem_destroy(tables); pthread_mutex_destroy(rest.lock); printf(所有顾客已服务完毕!\n); return 0; }15. 线程调试技巧GDB调试线程gdb ./a.out (gdb) run (gdb) info threads # 查看所有线程 (gdb) thread 2 # 切换到线程2 (gdb) bt # 查看线程调用栈 (gdb) thread apply all bt # 查看所有线程调用栈调试建议编译带调试信息gcc -g -pthread使用线程局部变量避免全局变量冲突添加调试输出打印线程ID和状态逐步测试先测试单线程再测试多线程16.总结多线程编程的关键在于正确处理共享资源的同步与互斥问题。合理使用互斥锁和信号量可以避免竞态条件和死锁同时需要注意线程的生命周期管理和资源回收。在实际开发中应根据具体需求选择合适的线程模型和同步机制。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

站长工具爱站海门网站开发

Axure RP 11中文界面配置终极攻略:5分钟搞定Mac汉化设置 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包,不定期更新。支持 Axure 9、Axure 10。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn …

张小明 2026/3/5 3:58:47 网站建设

网站留言板的作用企业logo设计免费生成器

黑苹果配置神器SSDTTime:3分钟搞定复杂补丁生成 【免费下载链接】SSDTTime SSDT/DSDT hotpatch attempts. 项目地址: https://gitcode.com/gh_mirrors/ss/SSDTTime 还在为黑苹果配置中的DSDT补丁而烦恼吗?每次面对复杂的硬件兼容性问题都感到无从…

张小明 2026/3/5 3:58:47 网站建设

做网站用微信收款还是支付宝建设手机网站设计

Stable Diffusion WebUI Forge图像质量评估技术深度解析 【免费下载链接】stable-diffusion-webui-forge 项目地址: https://gitcode.com/GitHub_Trending/st/stable-diffusion-webui-forge 在AI图像生成技术快速发展的今天,如何科学评估生成图像的质量已成…

张小明 2026/3/5 3:58:48 网站建设

wordpress网站从零营销技巧和营销方法

SQL与XML的融合:探索数据处理新境界 1. 引言 在互联网和Web技术不断发展的今天,可扩展标记语言(XML)和结构化查询语言(SQL)成为了处理和管理数据的重要工具。XML是一种用于表示和交换结构化数据的标准语言,而SQL则是定义、访问和更新关系数据库中结构化数据的标准语言…

张小明 2026/3/5 3:58:48 网站建设

秦皇岛网站建设公司重庆网红打卡点有哪些地方

网络安全工具psad与fwsnort的应用与集成 1. psad的主动响应机制 psad是一款强大的网络安全工具,它能够根据监测到的网络活动动态添加和删除iptables规则,以实现对恶意IP地址的主动响应。 1.1 规则添加示例 在一次扫描中,psad监测到来自144.202.X.X的66个UDP数据包后,添…

张小明 2026/3/5 3:58:49 网站建设

网站平台建设合同社交电商怎么做赚钱

基于无权重系数占空比模型预测转矩永磁同步电机控制,主体采用matlab function模块编程,与c语言接近,便于实物移植。 【提供参考论文】 相比于传统模型预测转矩控制性能提高很多!!可赠送传统模型预测转矩控制模型进行对…

张小明 2026/3/5 3:58:51 网站建设