神奇的pthread_join

本文介绍pthread_join函数的应用及线程间信号传递的方法。通过一个实例展示了如何使用pthread_join等待线程结束,并利用信号量实现主线程与子线程间的通信。

函数pthread_join用来等待一个线程的结束。函数原型为:
  extern int pthread_join __P ((pthread_t __th, void**__thread_return));
  第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们下面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。另外需要说明的是,一个线程不能被多个线程等待,也就是说对一个线程只能调用一次pthread_join,否则只有一个能正确返回,其他的将返回ESRCH 错误。

下面是改进过后的例子

//signaltest.c
//	子线程阻塞,等待信号,然后输出字符串
//	主线程从键盘录入字符,给子线程发信号

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <time.h> pthread_t tid; sigset_t set; void myfunc() { printf("hello/n"); } static void* mythread(void *p) { int signum; while(1){ sigwait(&set,&signum); if(SIGUSR1 == signum) myfunc(); if(SIGUSR2 == signum) { printf("I will sleep 2 second and exit/n"); sleep(2); break; } } } int main() { char tmp; void* status; sigemptyset(&set); sigaddset(&set,SIGUSR1); sigaddset(&set,SIGUSR2); sigprocmask(SIG_SETMASK,&set,NULL); pthread_create(&tid,NULL,mythread,NULL); while(1) { printf(":"); scanf("%c",&tmp); if('a' == tmp) { pthread_kill(tid,SIGUSR1); //发送SIGUSR1,打印字符串。 } else if('q'==tmp) { pthread_kill(tid,SIGUSR2); //发出SIGUSR2信号,让线程退出,如果发送SIGKILL,线程将直接退出。 pthread_join(tid,&status); //等待线程tid执行完毕,这里阻塞。 printf("finish/n"); break; } else continue; } return 0; }
在多线程编程中,`pthread_create` 和 `pthread_join` 是 POSIX 线程(pthread)API 中两个非常重要的函数。它们分别用于创建线程和等待线程的结束。 --- ### 1. `pthread_create` 该函数用于创建一个新的线程。其原型如下: ```c int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); ``` - `thread`:用于存储新创建线程的标识符。 - `attr`:线程属性,通常设为 `NULL` 表示使用默认属性。 - `start_routine`:线程执行的函数。 - `arg`:传递给线程函数的参数。 --- ### 2. `pthread_join` 该函数用于等待一个线程结束。其原型如下: ```c int pthread_join(pthread_t thread, void **retval); ``` - `thread`:要等待的线程标识符。 - `retval`:用来接收线程退出时的返回值(可以为 `NULL`)。 --- ### 示例代码 以下是一个使用 `pthread_create` 和 `pthread_join` 的简单示例: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> // 线程执行的函数 void* thread_function(void* arg) { int thread_num = *((int*)arg); printf("线程 %d 正在运行...\n", thread_num); pthread_exit((void*)"Thank you for the CPU time!"); // 线程退出 } int main() { pthread_t thread1, thread2; int num1 = 1, num2 = 2; int result; void* return_value; // 创建第一个线程 result = pthread_create(&thread1, NULL, thread_function, (void*)&num1); if (result) { fprintf(stderr, "线程1创建失败!错误代码:%d\n", result); exit(-1); } // 创建第二个线程 result = pthread_create(&thread2, NULL, thread_function, (void*)&num2); if (result) { fprintf(stderr, "线程2创建失败!错误代码:%d\n", result); exit(-1); } // 等待线程1结束 pthread_join(thread1, &return_value); printf("线程1返回值: %s\n", (char*)return_value); // 等待线程2结束 pthread_join(thread2, &return_value); printf("线程2返回值: %s\n", (char*)return_value); printf("主线程结束。\n"); return 0; } ``` --- ### 代码解释 - `pthread_create` 创建了两个线程,它们分别执行 `thread_function` 函数。 - `thread_function` 打印出线程编号并返回一个字符串。 - `pthread_join` 会阻塞主线程,直到指定的线程结束。 - 线程返回的值通过 `pthread_exit` 设置,并通过 `pthread_join` 获取。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值