本文介绍了pthread_join()用于异步线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的演示程序,以便我可以理解pthread_join()功能.

I have written a simple demonstration program so that I can understand the pthread_join() function.

我知道如何使用pthread_condition_wait()函数来允许异步线程,但是我试图了解如何使用pthread_join()函数来完成类似的工作.

I know how to use the pthread_condition_wait() function to allow asynchronous threading but I'm trying to understand how I can do similar work using the pthread_join() function.

在下面的程序中,我将 Thread 1s ID传递给 Thread 2s 函数.在 Thread 2s 函数内部,我调用pthread_join()函数并传入 Thread 1s ID.我希望这会使线程1 先运行,然后使线程2 再运行,但是我得到的是它们都同时运行.

In the program below I pass Thread 1s ID to Thread 2s function. Inside Thread 2s function I call the pthread_join() function and pass in Thread 1s ID. I expected this to cause Thread 1 to run first and then for Thread 2 to run second, but what I'm getting is that they both run concurrently.

这是因为一次只有一个线程可以使用pthread_join()函数,并且当我从主线程调用它时,我已经在使用pthread_join()函数了吗?

Is this because only one thread can use the pthread_join() function at a time and that I am already using the pthread_join() function when I call it from the main thread?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionCount1();
void *functionCount2(void*);

int main()
{

    /*
        How to Compile
         gcc -c foo
         gcc -pthread -o foo foo.o
    */

    printf("\n\n");

    int rc;
    pthread_t thread1, thread2;

    /* Create two thread --I took out error checking for clarity*/
    pthread_create( &thread1, NULL, &functionCount1, NULL)
    pthread_create( &thread2, NULL, &functionCount2, &thread1)

    pthread_join( thread1, NULL);
    pthread_join( thread2, NULL);

    printf("\n\n");
    exit(0);
}

void *functionCount1()
{

    printf("\nFunction 1");
        sleep(5);
    printf("\nFunction 1");

    return(NULL);
}

void *functionCount2(void* argument)
{

    pthread_t* threadID = (pthread_t*) argument;

    pthread_join(*threadID, NULL);

    printf("\nFunction 2");
        sleep(5);
    printf("\nFunction 2");

    return(NULL);
}

输出:

推荐答案

使用pthread_create()创建线程时,两个线程同时开始执行,并且执行顺序没有固定的顺序.顺序取决于操作系统调度和可用处理器的数量等.无论如何,这是无法确定的,无论如何这是线程的重点.

When threads are created using pthread_create(), both threads start execution concurrently and there's no fixed ordering on their order of execution. The order depends on the OS scheduling and number of processors available etc. In any case, that can't be determined and that's the point of threads anyway.

调用pthread_join()时,调用者等待与之连接的线程终止时会发生什么.因此,主线程等待thread1终止,然后等待thread2.但是,当main等待thread1时,thread2可能已经完成执行并已经终止.在这种情况下,pthread_join(thread2, NULL);将立即返回.

What happens when you call pthread_join(), the caller waits for thread that join with to terminate. So main thread waits for thread1 to terminate and then for thread2. But while main waits for thread1, thread2 might have finished execution and terminated already. In this case, pthread_join(thread2, NULL); would return immediately.

您的代码中存在更大的问题.您正在将thread1的ID传递给thread2.但是,如果thread1thread2开始之前完成了执行,那么您将使用无效的线程标识符导致未定义的行为.另一个问题是您的主线程和thread2都试图与thread1联接.简而言之,pthread_join()不是在多个线程之间进行同步的正确工具.如您所述,您正在为此使用条件变量/互斥量.

You have a bigger problem in your code. You are passing thread1's ID to thread2. But if thread1 finished execution before thread2 starts, then you are going be using an invalid thread identifier, leading to undefined behaviour. Another problem is that your main thread and thread2 both trying to join with thread1. In short, pthread_join() is not the right tool for synchronization between multiple threads. As you stated, you be using conditional variable/mutexes for this purpose.

我建议您从thread2中删除pthread_join(),以修复未定义的行为,如果要串行执行线程,则必须一个接一个地创建线程,并让它们等待上一个线程终止( pthread_join()).但是,这样做几乎没有实际用途.或者让线程在执行任何操作之前先等待条件变量,然后您可以使用条件变量使线程按照所需的顺序进行通信.

I suggest, you remove pthread_join() from thread2, to fix undefined behaviour and if you want serial execution of threads, then you have to create threads one after another and let them and wait for the previous thread to terminate (pthread_join()). But there's very little practical use in doing this. Or let the threads wait on a conditional variable before doing anything and you can make the threads communicate in the order you desire using conditional variables.

这篇关于pthread_join()用于异步线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:01