WordPress集成Claude AI:构建智能内容创作技术栈的实践指南
2026/5/13 2:21:11
Linux 中,线程被称为 “轻量级进程(LWP,Light Weight Process)”,线程属于进程—— 一个进程可以包含多个线程,所有线程共享进程的资源(如内存空间、文件描述符、信号处理等),同时拥有独立的执行流。
与进程一致,线程的核心价值是实现并发执行,尤其适合处理:
| 维度 | 核心描述 |
|---|---|
| 资源分配 | 进程是系统最小的资源分配单位(如内存、CPU 时间片);线程是系统最小的执行单位 |
| 层级关系 | 进程内的所有线程是平级关系,无父子之分;进程默认包含一个 “主线程”(main 函数对应的线程) |
| 资源范围 | 线程共享进程的全局资源(堆、全局变量、文件描述符),但拥有独立的栈区(默认 8MB) |
| 特性 | 线程 | 进程 |
|---|---|---|
| 资源共享 | 共享进程资源(堆、全局变量等),仅栈区独立 | 资源完全独立(虚拟地址空间隔离) |
| 稳定性 | 一个线程崩溃会导致整个进程崩溃 | 单个进程崩溃不影响其他进程 |
| 创建开销 | 仅需开辟独立栈区(8MB),开销极低 | 需创建完整虚拟地址空间(3GB),开销大 |
| 并发度 | 高(切换成本低) | 低(切换需切换地址空间) |
| 通信方式 | 直接读写共享变量(需同步) | 需 IPC(管道、消息队列、共享内存等) |
Linux 线程编程遵循 POSIX 标准(pthread 库),核心流程为:创建多线程→线程空间执行任务→线程资源回收(线程退出后栈区默认不释放,需手动 / 自动回收)
bash
运行
# 查看线程的PID、PPID、LWP(轻量级进程ID)、状态、命令 ps -eLo pid,ppid,lwp,stat,comm # 更详细的线程信息(含线程ID、CPU占用等) ps -eLf线程 / 进程的工作路径控制依赖以下函数(常与线程任务结合):
c
运行
// 获取当前工作路径 char *getcwd(char *buf, size_t size); // 参数:buf-存储路径的数组;size-数组最大长度 // 返回值:成功返回buf指针,失败返回NULL // 切换工作路径 int chdir(const char *path); // 参数:path-目标路径(绝对/相对) // 返回值:成功0,失败-1c
运行
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);start_routine函数;thread:输出参数,存储新线程的 ID(需提前定义);attr:线程属性(如栈大小、分离属性),默认传NULL(使用默认属性);start_routine:线程的执行函数(回调函数),格式为void* 函数名(void*);arg:传给回调函数的参数(void * 类型,可传递任意数据);c
运行
pthread_t pthread_self(void);unsigned long int,打印用%lu)。c
运行
void pthread_exit(void *retval);retval:线程退出的返回值(“临死遗言”),可被pthread_join获取;pthread_exit仅退出自身,不影响其他子线程。c
运行
int pthread_cancel(pthread_t thread);thread:要取消的线程 ID;c
运行
int pthread_join(pthread_t thread, void **retval);thread:要回收的线程 ID;retval:输出参数,存储线程的退出返回值(对应pthread_exit的参数);c
运行
int pthread_detach(pthread_t thread);thread:要设置的线程 ID(可传pthread_self()设置自身);pthread_join,避免僵尸线程(线程退出后资源未释放);pthread_join回收该线程。c
运行
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> // 线程执行函数 void* thread_func(void* arg) { char *msg = (char*)arg; printf("子线程ID:%lu,参数:%s\n", pthread_self(), msg); sleep(2); // 线程退出,返回值为"thread exit" pthread_exit((void*)"thread exit"); } int main() { pthread_t tid; int ret; void *retval; // 创建线程 ret = pthread_create(&tid, NULL, thread_func, (void*)"hello thread"); if (ret != 0) { perror("pthread_create failed"); exit(1); } printf("主线程ID:%lu,创建子线程ID:%lu\n", pthread_self(), tid); // 阻塞回收线程,获取返回值 ret = pthread_join(tid, &retval); if (ret != 0) { perror("pthread_join failed"); exit(1); } printf("子线程退出,返回值:%s\n", (char*)retval); return 0; }bash
运行
# 编译(必须链接pthread库) gcc thread_demo.c -o thread_demo -lpthread # 运行 ./thread_demoplaintext
主线程ID:140709267896000,创建子线程ID:140709259507456 子线程ID:140709259507456,参数:hello thread 子线程退出,返回值:thread exitc
运行
#include <stdio.h> #include <pthread.h> #include <unistd.h> void* thread_func(void* arg) { // 设置自身为分离属性 pthread_detach(pthread_self()); printf("子线程ID:%lu,执行任务\n", pthread_self()); sleep(2); printf("子线程退出,资源自动回收\n"); pthread_exit(NULL); } int main() { pthread_t tid; pthread_create(&tid, NULL, thread_func, NULL); // 主线程等待3秒,确保子线程执行完成 sleep(3); printf("主线程退出\n"); return 0; }-lpthread,否则报 “undefined reference to pthread_create”;pthread_exit主动退出;pthread_exit仅退出自身,用exit会终止整个进程;pthread_join回收,否则会产生 “僵尸线程”;strtok),优先使用线程安全版本(如strtok_r)。