pthread(一) —— pthread_create & pthread_join

本文详细介绍了pthread_create和pthread_join两个核心函数的使用方法,包括它们的参数、返回值及注意事项。通过阅读本文,读者可以了解到如何在类Unix操作系统中创建线程以及如何等待线程结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pthread_create


pthread_create是类Unix操作系统(UnixLinuxMacOS X等)的创建线程的函数。头文件 :#include<pthread.h>

函数定义:intpthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void *arg);

编译链接参数:-pthread

返回值:0创建成功;创建失败返回错误编号,同时*thread内容未定义

参数:

第一个参数为指向线程标识符的指针。

第二个参数用来设置线程属性。

第三个参数是线程运行函数的起始地址。

最后一个参数是运行函数的参数。



pthread_join

函数pthread_join用来等待一个线程的结束。头文件: #include<pthread.h>

函数定义: intpthread_join(pthread_t thread, void **retval);

描述:pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。

参数 :thread:线程标识符,即线程ID,标识唯一线程。retval:用户定义的指针,用来存储被等待线程的返回值。

返回值 : 0代表成功。失败,返回的则是错误号。


注:

还有一些线程,更喜欢自己来清理退出的状态,他们也不愿意主线程调用pthread_join来等待他们。我们将这一类线程的属性称为detached。如果我们在调用pthread_create()函数的时候将属性设置为NULL,则表明我们希望所创建的线程采用默认的属性,也就是joinable

#include &lt;stdio.h&gt; #include &lt;pthread.h&gt; #include &lt;errno.h&gt; #include &lt;signal.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/shm.h&gt; #include &lt;sys/sem.h&gt; #include &lt;sys/ipc.h&gt; #include &quot;data_global.h&quot; extern pthread_cond_t cond_sqlite; extern pthread_cond_t cond_analysis; extern pthread_cond_t cond_client_send; extern pthread_cond_t cond_uart_cmd; extern pthread_cond_t cond_client_request; extern pthread_cond_t cond_infrared; extern pthread_cond_t cond_buzzer; extern pthread_cond_t cond_led; extern pthread_cond_t cond_camera; extern pthread_cond_t cond_sms; extern pthread_cond_t cond_refresh; extern pthread_cond_t cond_refresh_updata; extern pthread_mutex_t mutex_slinklist; extern pthread_mutex_t mutex_sqlite; extern pthread_mutex_t mutex_analysis; extern pthread_mutex_t mutex_client_send; extern pthread_mutex_t mutex_client_receive; extern pthread_mutex_t mutex_uart_cmd; extern pthread_mutex_t mutex_client_request; extern pthread_mutex_t mutex_infrared; extern pthread_mutex_t mutex_buzzer; extern pthread_mutex_t mutex_led; extern pthread_mutex_t mutex_camera; extern pthread_mutex_t mutex_sms; extern pthread_mutex_t mutex_refresh; extern pthread_mutex_t mutex_refresh_updata; extern pthread_mutex_t mutex_global; extern pthread_mutex_t mutex_linklist; extern int dev_infrared_fd; extern int dev_buzzer_fd; extern int dev_led_fd; extern int dev_camera_fd; extern int dev_sms_fd; extern int dev_uart_fd; extern int msgid; extern int shmid; extern int semid; extern void Create_table (); extern struct env_info_clien_addr all_info_RT; pthread_t id_sqlite, id_analysis, id_transfer, id_client_send, id_client_receive, id_uart_cmd, id_client_request, id_infrared, id_buzzer, id_led, id_camera, id_sms, id_refresh; void ReleaseResource (int signo) { pthread_mutex_destroy (&amp;mutex_linklist); pthread_mutex_destroy (&amp;mutex_global); pthread_mutex_destroy (&amp;mutex_refresh_updata); pthread_mutex_destroy (&amp;mutex_refresh); pthread_mutex_destroy (&amp;mutex_sms); pthread_mutex_destroy (&amp;mutex_camera); pthread_mutex_destroy (&amp;mutex_led); pthread_mutex_destroy (&amp;mutex_buzzer); pthread_mutex_destroy (&amp;mutex_infrared); pthread_mutex_destroy (&amp;mutex_client_request); pthread_mutex_destroy (&amp;mutex_uart_cmd); pthread_mutex_destroy (&amp;mutex_analysis); pthread_mutex_destroy (&amp;mutex_client_send); pthread_mutex_destroy (&amp;mutex_client_receive); pthread_mutex_destroy (&amp;mutex_sqlite); pthread_mutex_destroy (&amp;mutex_slinklist); pthread_cond_destroy (&amp;cond_client_send); pthread_cond_destroy (&amp;cond_refresh_updata); pthread_cond_destroy (&amp;cond_refresh); pthread_cond_destroy (&amp;cond_sms); pthread_cond_destroy (&amp;cond_camera); pthread_cond_destroy (&amp;cond_led); pthread_cond_destroy (&amp;cond_buzzer); pthread_cond_destroy (&amp;cond_infrared); pthread_cond_destroy (&amp;cond_client_request); pthread_cond_destroy (&amp;cond_uart_cmd); pthread_cond_destroy (&amp;cond_analysis); pthread_cond_destroy (&amp;cond_sqlite); msgctl (msgid, IPC_RMID, NULL); shmctl (shmid, IPC_RMID, NULL); pthread_cancel (id_refresh); pthread_cancel (id_sms); pthread_cancel (id_camera); pthread_cancel (id_led); pthread_cancel (id_buzzer); pthread_cancel (id_infrared); pthread_cancel (id_client_request); pthread_cancel (id_uart_cmd); pthread_cancel (id_transfer); pthread_cancel (id_analysis); pthread_cancel (id_client_send); pthread_cancel (id_client_receive); pthread_cancel (id_sqlite); close (dev_camera_fd); close (dev_led_fd); close (dev_buzzer_fd); close (dev_infrared_fd); close (dev_sms_fd); close (dev_uart_fd); printf (&quot;All quit\n&quot;); exit(0); } void setLimit (int sto_no, float temMAX, float temMIN, float humMAX, float humMIN, float illMAX, float illMIN) { if (sto_no &gt;= 0 &amp;&amp; (sto_no &lt;=STORAGE_NUM - 1)) { all_info_RT.storage_no[sto_no].temperatureMAX = temMAX; all_info_RT.storage_no[sto_no].temperatureMIN = temMIN; all_info_RT.storage_no[sto_no].humidityMAX = humMAX; all_info_RT.storage_no[sto_no].humidityMIN = humMIN; all_info_RT.storage_no[sto_no].illuminationMAX = illMAX; all_info_RT.storage_no[sto_no].illuminationMIN = illMIN; } } int main(int argc, char **argv) { #if 1 / Create_table (); // setLimit (1, 50, 5, 50, 10, 500, 10); setLimit (2, 50, 5, 50, 10, 500, 10); pthread_mutex_init (&amp;mutex_slinklist, NULL);// pthread_mutex_init (&amp;mutex_sqlite, NULL); pthread_mutex_init (&amp;mutex_analysis, NULL); pthread_mutex_init (&amp;mutex_uart_cmd, NULL); pthread_mutex_init (&amp;mutex_client_request, NULL); pthread_mutex_init (&amp;mutex_infrared, NULL); pthread_mutex_init (&amp;mutex_buzzer, NULL); pthread_mutex_init (&amp;mutex_led, NULL); pthread_mutex_init (&amp;mutex_camera, NULL); pthread_mutex_init (&amp;mutex_sms, NULL); pthread_mutex_init (&amp;mutex_refresh, NULL); pthread_mutex_init (&amp;mutex_refresh_updata, NULL); pthread_mutex_init (&amp;mutex_global, NULL); pthread_mutex_init (&amp;mutex_linklist, NULL); pthread_mutex_init (&amp;mutex_client_send, NULL); pthread_cond_init (&amp;cond_client_send, NULL); pthread_cond_init (&amp;cond_sqlite, NULL); pthread_cond_init (&amp;cond_analysis, NULL); pthread_cond_init (&amp;cond_uart_cmd, NULL); pthread_cond_init (&amp;cond_client_request, NULL); pthread_cond_init (&amp;cond_infrared, NULL); pthread_cond_init (&amp;cond_buzzer, NULL); pthread_cond_init (&amp;cond_led, NULL); pthread_cond_init (&amp;cond_camera, NULL); pthread_cond_init (&amp;cond_sms, NULL); pthread_cond_init (&amp;cond_refresh, NULL); pthread_cond_init (&amp;cond_refresh_updata, NULL); //signal (SIGINT, ReleaseResource); // pthread_create (&amp;id_sqlite, 0, pthread_sqlite, NULL); sleep (1); pthread_create (&amp;id_analysis, 0, pthread_analysis, NULL); pthread_create (&amp;id_client_send, 0, pthread_client_send, NULL); pthread_create (&amp;id_transfer, 0, pthread_transfer, NULL); sleep (1); pthread_create (&amp;id_uart_cmd, 0, pthread_uart_cmd, NULL); pthread_create (&amp;id_client_request, 0, pthread_client_request, NULL); pthread_create (&amp;id_infrared, 0, pthread_infrared, NULL); pthread_create (&amp;id_buzzer, 0, pthread_buzzer, NULL); pthread_create (&amp;id_led, 0, pthread_led, NULL); pthread_create (&amp;id_camera, 0, pthread_camera, NULL); pthread_create (&amp;id_sms, 0, pthread_sms, NULL); pthread_create (&amp;id_refresh, 0, pthread_refresh, NULL); // pthread_join (id_sqlite, NULL); pthread_join (id_client_send, NULL); printf (&quot;g1\n&quot;); pthread_join (id_analysis, NULL); printf (&quot;g2\n&quot;); pthread_join (id_transfer, NULL); printf (&quot;g3\n&quot;); pthread_join (id_uart_cmd, NULL); printf (&quot;g4\n&quot;); pthread_join (id_client_request, NULL); printf (&quot;g5\n&quot;); pthread_join (id_infrared, NULL); printf (&quot;g6\n&quot;); pthread_join (id_buzzer, NULL); printf (&quot;g7\n&quot;); pthread_join (id_led, NULL); printf (&quot;g8\n&quot;); pthread_join (id_camera, NULL); printf (&quot;g9\n&quot;); pthread_join (id_sms, NULL); printf (&quot;g10\n&quot;); pthread_join (id_refresh, NULL); printf (&quot;g11\n&quot;); return 0; }
最新发布
07-26
&lt;think&gt;好的,用户现在想了解pthread_createpthread_joinpthread_detach这三个函数。之前用户询问过在GDB调试多线程程序时卡在创建线程的问题,现在可能是在进步学习多线程编程的基础知识,或者是在解决线程管理相关的问题。需要确保回答清晰,区分这三个函数的用途和区别,尤其是join和detach的不同,因为这两个函数容易混淆。 首先,我需要回顾这三个函数的基本功能和用法。pthread_create用于创建线程,参数包括线程标识符、属性、启动函数和参数。用户可能想知道如何正确使用它,比如传递参数时的注意事项,或者属性设置的影响。然后,pthread_join用于等待线程结束并回收资源,而pthread_detach则是让线程结束后自动释放资源,不需要join。如果用户之前遇到线程卡住的问题,可能和没有正确使用join或detach有关,导致资源泄露或者主线程提前退出。 需要强调线程分离的重要性,避免僵尸线程。同时,要指出使用detach后不能再join,否则会导致错误。此外,可能用户在实际编码中遇到线程未正确回收的问题,或者对线程的生命周期管理不太清楚。这时候,给出示例代码会有帮助,比如展示如何正确创建线程,并使用join或detach来管理。 另外,用户可能想知道这三个函数之间的配合使用场景。比如,何时该用join,何时用detach。比如,主线程需要等待子线程完成时使用join,而如果子线程独立运行,不需要等待,则用detach。同时,要提醒如果既不join也不detach,可能会导致资源泄露,这也是常见错误点。 可能还需要提到错误处理,比如检查这些函数的返回值,确保线程创建和管理成功。此外,线程属性如设置为detach状态,可以在创建时就分离,避免后续调用detach的需要。这些细节能帮助用户更全面地理解线程管理。 最后,总结对比这三个函数,确保用户理解它们各自的作用和使用场景。这样用户在编写多线程程序时,能够正确管理线程的生命周期,避免常见的陷阱,比如资源泄露或者程序异常终止。&lt;/think&gt;以下是`pthread_create`、`pthread_join`和`pthread_detach`的详细对比说明: --- ### **1. `pthread_create` - 线程创建** #### **函数原型** ```c int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); ``` #### **核心功能** - 创建新线程,执行`start_routine`函数 - `attr`参数控制线程属性(可设置为`NULL`使用默认属性) - `arg`是传递给线程函数的参数(必须是全局变量或堆内存) #### **示例** ```c void* thread_func(void* arg) { printf(&quot;Thread ID: %lu\n&quot;, (unsigned long)pthread_self()); return NULL; } int main() { pthread_t tid; pthread_create(&amp;tid, NULL, thread_func, NULL); // ... } ``` --- ### **2. `pthread_join` - 线程同步** #### **函数原型** ```c int pthread_join(pthread_t thread, void **retval); ``` #### **核心功能** - 阻塞当前线程,等待目标线程终止 - 回收线程资源(必须调用,否则会产生&quot;僵尸线程&quot;) - 获取线程返回值(通过`retval`参数) #### **示例** ```c void* thread_func(void* arg) { int* result = malloc(sizeof(int)); *result = 42; return result; } int main() { pthread_t tid; void* ret_val; pthread_create(&amp;tid, NULL, thread_func, NULL); pthread_join(tid, &amp;ret_val); printf(&quot;Thread returned: %d\n&quot;, *(int*)ret_val); free(ret_val); } ``` --- ### **3. `pthread_detach` - 线程分离** #### **函数原型** ```c int pthread_detach(pthread_t thread); ``` #### **核心功能** - 将线程标记为&quot;分离状态&quot; - 分离后的线程终止时会自动释放资源 - 分离后不可再调用`pthread_join` #### **示例** ```c void* thread_func(void* arg) { // 长时间运行的任务... return NULL; } int main() { pthread_t tid; pthread_create(&amp;tid, NULL, thread_func, NULL); pthread_detach(tid); // 分离线程 // 主线程无需等待即可继续执行 while(1) { /* 主线程任务 */ } } ``` --- ### **对比总结** | 特性 | `pthread_create` | `pthread_join` | `pthread_detach` | |---------------------|------------------------|-----------------------------|-----------------------------| | 主要作用 | 创建新线程 | 等待线程结束并回收资源 | 自动回收线程资源 | | 阻塞行为 | 非阻塞 | 阻塞调用线程 | 非阻塞 | | 资源回收 | 不负责 | 显式回收 | 隐式自动回收 | | 返回值获取 | 无 | 可以获取 | 无法获取 | | 调用次数限制 | 每个线程1次 | 每个线程最多1次 | 每个线程最多1次 | | 典型使用场景 | 启动并发任务 | 需要等待线程结果 | 无需关心线程结束状态 | --- ### **最佳实践** 1. **必须二选**:对每个线程必须调用`pthread_join`或`pthread_detach` 2. **创建即分离**:可以在创建时设置分离属性: ```c pthread_attr_t attr; pthread_attr_init(&amp;attr); pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_DETACHED); pthread_create(&amp;tid, &amp;attr, thread_func, NULL); ``` 3. **错误处理**:检查所有pthread函数的返回值(0表示成功,非0为错误码) --- ### **常见问题** 1. **双重join错误**: ```c pthread_join(tid, NULL); pthread_join(tid, NULL); // 会导致未定义行为 ``` 2. **分离后join**: ```c pthread_detach(tid); pthread_join(tid, NULL); // 错误:返回EINVAL ``` 3. **主线程提前退出**: ```c int main() { pthread_t tid; pthread_create(&amp;tid, NULL, thread_func, NULL); return 0; // 主线程退出可能导致进程终止 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值