pthread_join函数

来源:互联网 发布:知乎专栏文章怎么收藏 编辑:程序博客网 时间:2024/04/27 02:33
函数pthread_join用来等待一个线程的结束,线程间同步的操作。头文件 : #include <pthread.h>
函数定义: int pthread_join(pthread_t thread, void **retval);
描述 :pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。
参数 :thread: 线程标识符,即线程ID,标识唯一线程。retval: 用户定义的指针,用来存储被等待线程的返回值。
返回值 : 0代表成功。 失败,返回的则是错误号。


函数应用

编辑
linux中的应用
在Linux中,默认情况下是在一个线程被创建后,必须使用此函数对创建的线程进行资源回收,但是可以设置Threads attributes来设置当一个线程结束时,直接回收此线程所占用的系统资源,详细资料查看Threads attributes。
其实在Linux中,新建的线程并不是在原先的进程中,而是系统通过一个系统调用clone()。该系统调用copy了一个和原先进程完全一样的进程,并在这个进程中执行线程函数。不过这个copy过程和fork不一样。 copy后的进程和原先的进程共享了所有的变量,运行环境。这样,原先进程中的变量变动在copy后的进程中便能体现出来。
pthread_join的应用
pthread_join使一个线程等待另一个线程结束。
代码中如果没有pthread_join主线程会很快结束从而使整个进程结束,从而使创建的线程没有机会开始执行就结束了。加入pthread_join后,主线程会一直等待直到等待的线程结束自己才结束,使创建的线程有机会执行。
所有线程都有一个线程号,也就是Thread ID。其类型为pthread_t。通过调用pthread_self()函数可以获得自身的线程号。

使用范例

编辑
一个线程的结束有两种途径,一种是象我们下面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。另外需要说明的是,一个线程不能被多个线程等待,也就是说对一个线程只能调用一次pthread_join,否则只有一个能正确返回,其他的将返回ESRCH 错误。
在Linux中,默认情况下是在一个线程被创建后,必须使用此函数对创建的线程进行资源回收,但是可以设置Threads attributes来设置当一个线程结束时,直接回收此线程所占用的系统资源,详细资料查看Threads attributes。
范例:
//signaltest.c
  // 子线程阻塞,等待信号,然后输出字符串
  // 主线程从键盘录入字符,给子线程发信号。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<pthread.h>
#include<time.h>
pthread_ttid; sigset_tset;
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("Iwillsleep2secondandexit\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)
        {
            //发出SIGUSR2信号,让线程退出,如果发送SIGKILL,线程将直接退出。
            pthread_kill(tid,SIGUSR2);
            //等待线程tid执行完毕,这里阻塞。
            pthread_join(tid,&status);
            printf("finish\n");
            break;
        }
        else
            continue;
    }
    return0;
}
运行结果:
// 如果输入a,子线程打印"hello",主程序继续等待输入;
// 如果输入q,主程序等待子程序结束。子线程打印"I will sleep 2 second and exit",并延时两秒后结束。主线程随之打印"finish",程序结束。
在前面我们提到,可以通过pthread_join()函数来使主线程阻塞等待其他线程退出,这样主线程可以清理其他线程的环境。但是还有一些线程,更喜欢自己来清理退出的状态,他们也不愿意主线程调用pthread_join来等待他们。我们将这一类线程的属性称为detached。如果我们在调用pthread_create()函数的时候将属性设置为NULL,则表明我们希望所创建的线程采用默认的属性,也就是joinable。如果需要将属性设置为detached,则参考下面的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void*start_run(void*arg)
{
//dosomework
}
 
intmain()
{
pthread_tthread_id;
pthread_attr_tattr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&thread_id,&attr,start_run,NULL);
pthread_attr_destroy(&attr);
sleep(5);
exit(0);
}
在线程设置为joinable后,可以调用pthread_detach()使之成为detached。但是相反的操作则不可以。还
有,如果线程已经调用pthread_join()后,则再调用pthread_detach()则不会有任何效果。


pthread_join函数及Linux线程

pthread_join使一个线程等待另一个线程结束。

 

代码中如果没有pthread_join主线程会很快结束从而使整个进程结束,从而使创建的线程没有机会开始执行就结束了。加入pthread_join后,主线程会一直等待直到等待的线程结束自己才结束,使创建的线程有机会执行。

 

所有线程都有一个线程号,也就是Thread ID。其类型为pthread_t。通过调用pthread_self()函数可以获得自身的线程号。

 

下面说一下如何创建一个线程。

 

通过创建线程,线程将会执行一个线程函数,该线程格式必须按照下面来声明:

 

       void * Thread_Function(void *)

 

创建线程的函数如下:

 

       int pthread_create(pthread_t *restrict thread,

 

              const pthread_attr_t *restrict attr,

 

              void *(*start_routine)(void*), void *restrict arg);

 

下面说明一下各个参数的含义:

 

thread:所创建的线程号。

 

attr:所创建的线程属性,这个将在后面详细说明。

 

start_routine:即将运行的线程函数。

 

art:传递给线程函数的参数。

 

下面是一个简单的创建线程例子:

 

#include <pthread.h>

 

#include <stdio.h>

 

/* Prints x’s to stderr. The parameter is unused. Does not return. */

 

void* print_xs (void* unused)

 

{

 

while (1)

 

fputc (‘x’, stderr);

 

return NULL;

 

}

 

/* The main program. */

 

int main ()

 

{

 

pthread_t thread_id;

 

/* Create a new thread. The new thread will run the print_xs

 

function. */

 

pthread_create (&thread_id, NULL, &print_xs, NULL);

 

/* Print o’s continuously to stderr. */

 

while (1)

 

fputc (‘o’, stderr);

 

return 0;

 

}

 

 

在编译的时候需要注意,由于线程创建函数在libpthread.so库中,所以在编译命令中需要将该库导入。命令如下:

 

gcc –o createthread –lpthread createthread.c

 

如果想传递参数给线程函数,可以通过其参数arg,其类型是void *。如果你需要传递多个参数的话,可以考虑将这些参数组成一个结构体来传递。另外,由于类型是void *,所以你的参数不可以被提前释放掉。

 

下面一个问题和前面创建进程类似,不过带来的问题回避进程要严重得多。如果你的主线程,也就是main函数执行的那个线程,在你其他县城推出之前就已经退出,那么带来的bug则不可估量。通过pthread_join函数会让主线程阻塞,直到所有线程都已经退出。

 

int pthread_join(pthread_t thread, void **value_ptr);

 

thread:等待退出线程的线程号。

 

value_ptr:退出线程的返回值。

 

下面一个例子结合上面的内容:

 

int main ()

 

{

 

pthread_t thread1_id;

 

pthread_t thread2_id;

 

struct char_print_parms thread1_args;

 

struct char_print_parms thread2_args;

 

/* Create a new thread to print 30,000 x’s. */

 

thread1_args.character = ’x’;

 

thread1_args.count = 30000;

 

pthread_create (&thread1_id, NULL, &char_print, &thread1_args);

 

/* Create a new thread to print 20,000 o’s. */

 

thread2_args.character = ’o’;

 

thread2_args.count = 20000;

 

pthread_create (&thread2_id, NULL, &char_print, &thread2_args);

 

/* Make sure the first thread has finished. */

 

pthread_join (thread1_id, NULL);

 

/* Make sure the second thread has finished. */

 

pthread_join (thread2_id, NULL);

 

/* Now we can safely return. */

 

return 0;

 

}

 

 

下面说一下前面提到的线程属性。

 

在我们前面提到,可以通过pthread_join()函数来使主线程阻塞等待其他线程退 出,这样主线程可以清理其他线程的环境。但是还有一些线程,更喜欢自己来清理退出的状态,他们也不愿意主线程调用pthread_join来等待他们。我 们将这一类线程的属性称为detached。如果我们在调用pthread_create()函数的时候将属性设置为NULL,则表明我们希望所创建的线 程采用默认的属性,也就是jionable。如果需要将属性设置为detached,则参考下面的例子:

 

#include <stdio.h>

 

#include <pthread.h>

 

void * start_run(void * arg)

 

{

 

        //do some work

 

}

 

int main()

 

{

 

        pthread_t thread_id;

 

        pthread_attr_t attr;

 

        pthread_attr_init(&attr);

 

        pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

 

        pthread_create(&thread_id,&attr,start_run,NULL);

 

        pthread_attr_destroy(&attr);

 

        sleep(5);

 

        exit(0);

 

}

 

 

在线程设置为joinable后,可以调用pthread_detach()使之成为detached。但是相反的操作则不可以。还有,如果线程已经调用pthread_join()后,则再调用pthread_detach()则不会有任何效果。

 

线程可以通过自身执行结束来结束,也可以通过调用pthread_exit()来结束线程的执行。另外,线程甲可以被线程乙被动结束。这个通过调用pthread_cancel()来达到目的。

 

int pthread_cancel(pthread_t thread);

 

       函数调用成功返回0。

 

当然,线程也不是被动的被别人结束。它可以通过设置自身的属性来决定如何结束。

 

线程的被动结束分为两种,一种是异步终结,另外一种是同步终结。异步终结就是当其他线程调用 pthread_cancel的时候,线程就立刻被结束。而同步终结则不会立刻终结,它会继续运行,直到到达下一个结束点(cancellation point)。当一个线程被按照默认的创建方式创建,那么它的属性是同步终结。

 

通过调用pthread_setcanceltype()来设置终结状态。

 

int pthread_setcanceltype(int type, int *oldtype);

 

state:要设置的状态,可以为PTHREAD_CANCEL_DEFERRED或者为PTHREAD_CANCEL_ASYNCHRONOUS。

 

那么前面提到的结束点又是如何设置了?最常用的创建终结点就是调用pthread_testcancel()的地方。该函数除了检查同步终结时的状态,其他什么也不做。

 

上面一个函数是用来设置终结状态的。还可以通过下面的函数来设置终结类型,即该线程可不可以被终结:

 

int pthread_setcancelstate(int state, int *oldstate);

 

       state:终结状态,可以为PTHREAD_CANCEL_DISABLE或者PTHREAD_CANCEL_ENABLE。具体什么含义大家可以通过单词意思即可明白。

 

最后说一下线程的本质。其实在Linux中,新建的线程并不是在原先的进程中,而是系统通过 一个系统调用clone()。该系统copy了一个和原先进程完全一样的进程,并在这个进程中执行线程函数。不过这个copy过程和fork不一样。 copy后的进程和原先的进程共享了所有的变量,运行环境。这样,原先进程中的变量变动在copy后的进程中便能体现出来。



0 0
原创粉丝点击