网站建设多少钱实惠湘潭磐石网络网页设计与制作课程简介

张小明 2026/3/12 20:04:02
网站建设多少钱实惠湘潭磐石网络,网页设计与制作课程简介,网站备案完成通知书,家装设计平台线程基本概念线程定义#xff1a;在 Linux 中#xff0c;线程属于某个进程#xff0c;是轻量级的执行单元。每个进程默认有一个主线程#xff0c;线程间是平级关系。作用#xff1a;实现并发执行#xff0c;提高资源利用率和响应速度。特征#xff1a;进程是最小资源分配…线程基本概念线程定义在 Linux 中线程属于某个进程是轻量级的执行单元。每个进程默认有一个主线程线程间是平级关系。作用实现并发执行提高资源利用率和响应速度。特征进程是最小资源分配单位线程是最小执行单位。线程共享进程资源如内存空间但拥有独立的栈区通常 8MB。稳定性较差若一个线程崩溃可能导致整个进程崩溃。创建开销小线程创建只需在进程空间中开辟新栈区而进程创建需分配更多资源如 3GB 空间。并发度高线程并发优于进程因为切换成本更低。线程与进程的区别资源共享线程共享进程资源全局变量、堆等进程资源独立。稳定性进程更稳定独立地址空间线程不稳定共享资源。创建开销线程开销小仅栈区进程开销大完整资源分配。并发度线程并发效率更高适合高并发场景。线程编程步骤POSIX创建线程使用pthread_create函数。线程操作在线程函数中执行任务。资源回收通过pthread_join或设置分离属性回收资源避免内存泄漏。查看线程信息在 Linux 终端可使用以下命令查看线程ps -eLf # 显示进程和线程信息 ps -elf # 详细列表线程相关函数详解下面逐一解释您列出的函数并提供代码示例。所有示例基于 C 语言使用 POSIX 线程库。获取线程 ID (pthread_t pthread_self(void))功能返回当前线程的 IDpthread_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; }线程退出 (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; }请求结束线程 (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; }线程资源回收 (int pthread_join(pthread_t thread, void **retval))功能阻塞等待指定线程结束并回收资源栈、状态等。参数thread目标线程 ID。retval接收线程退出状态需匹配pthread_exit的返回值。返回值成功返回 0失败返回非零错误码。示例见以上pthread_exit示例。设置分离属性 (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防止竞态条件。错误处理检查函数返回值使用perror或strerror诊断错
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

怎么看网站用的什么后台wordpress底部文件修改

抖音无水印下载神器:douyin_downloader完整使用教程 【免费下载链接】douyin_downloader 抖音短视频无水印下载 win编译版本下载:https://www.lanzous.com/i9za5od 项目地址: https://gitcode.com/gh_mirrors/dou/douyin_downloader 还在为抖音视…

张小明 2026/3/5 5:45:18 网站建设

网站建设可以在家做吗莱芜金点子广告手机版

第一章:量子编程入门与环境准备 量子计算正逐步从理论走向实践,掌握量子编程成为前沿开发者的重要技能。本章介绍如何搭建量子编程环境,并为后续算法实现打下基础。 选择量子开发框架 目前主流的量子计算开发框架包括Qiskit、Cirq和PennyLan…

张小明 2026/3/5 5:42:18 网站建设

网站设计中搜索界面怎么做20个中国风网站设计欣赏

LobeChat:构建节日营销智能助手的技术实践 在“双十一”、“618”这类全民购物节期间,用户涌入电商平台咨询优惠规则、比价信息和配送政策,客服系统往往不堪重负。而传统网页FAQ交互僵硬,无法满足个性化提问需求;自研A…

张小明 2026/3/5 5:42:21 网站建设

东西湖区网站建设公司如何建立单页网站

软件外包与人才服务型上市公司设计资金管理平台,需结合行业特性(项目制、人力成本为主、多客户结算、周期性收款等)和上市公司合规要求。以下是一个系统化的设计框架:一、核心目标资金可视化管理:实时监控现金流、账户…

张小明 2026/3/5 5:42:21 网站建设

沈阳建设工程信息网平台电话深圳seo公司

Winlator权限管理实战:3大核心技巧让手机秒变Windows工作站 【免费下载链接】winlator Android application for running Windows applications with Wine and Box86/Box64 项目地址: https://gitcode.com/GitHub_Trending/wi/winlator 你是否曾经因为手机无…

张小明 2026/3/5 5:42:22 网站建设

坦克大战网站开发课程设计报告iis7 发布静态网站

3步搞定USBToolBox:跨平台USB映射终极配置指南 【免费下载链接】tool the USBToolBox tool 项目地址: https://gitcode.com/gh_mirrors/too/tool USB端口映射是Hackintosh和系统优化中的关键技术难题,而USBToolBox正是解决这一难题的终极武器。这…

张小明 2026/3/5 5:42:22 网站建设