网站建设多少钱实惠湘潭磐石网络网页设计与制作课程简介
网站建设多少钱实惠湘潭磐石网络,网页设计与制作课程简介,网站备案完成通知书,家装设计平台线程基本概念线程定义#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诊断错