CefFlashBrowser完整使用指南:如何在2025年继续畅玩Flash游戏并管理存档
2026/6/3 9:46:13
线程与进程对比
创建线程
使用pthread_create函数:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);thread:返回线程ID。attr:线程属性(通常为NULL)。start_routine:线程执行的函数(回调函数)。arg:传递给回调函数的参数。获取线程ID
pthread_t pthread_self(void);返回当前线程ID,格式为unsigned long int(%lu)。
线程退出
pthread_exit(void *retval)retval为线程退出状态(如返回结果或错误码)。pthread_cancel(pthread_t thread)资源回收
pthread_join(pthread_t thread, void **retval)retval接收线程退出状态。pthread_detach(pthread_t thread)pthread_join)。ps -eLo pid,ppid,lwp,stat,commlwp:线程ID,stat:线程状态。ps -eLfpthread_detach后不可再调用pthread_join。示例代码片段:
void *thread_func(void *arg) { printf("Thread ID: %lu\n", pthread_self()); pthread_exit((void *)42); // 退出并返回状态 } pthread_t tid; pthread_create(&tid, NULL, thread_func, NULL); int *retval; pthread_join(tid, (void **)&retval); // 阻塞等待并回收