简体   繁体   English

使用pthread_cond_t表示执行结束

[英]Using pthread_cond_t to signal end of execution

I am using pthread_cond_t to signal the end of execution of child threads to the main thread. 我正在使用pthread_cond_t向主线程发出子线程执行结束的信号。 Since I'm not synchronizing the access to a shared resource, I wonder what the loop embracing pthread_cond_wait would be? 由于我不同步对共享资源的访问,因此我想知道包含pthread_cond_wait的循环是什么? Here's what I have: 这是我所拥有的:

pthread_mutex_t mutex;
pthread_cond_t cond;

int main(int argc, char *argv[])
{
        pthread_cond_init(&cond, NULL);
        cond = PTHREAD_COND_INITIALIZER;

        pthread_create(&tid1, NULL, func1, NULL);
        pthread_create(&tid2, NULL, func2, NULL);

        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        pthread_mutex_unlock(&mutex);

        //Join the thread that first completes

}

void *func1(void *arg)
{
        ....
        pthread_cond_signal(&cond);
        pthread_exit((void *) 1);
}

void *func2(void *arg)
{
        ....
        pthread_cond_signal(&cond);
        pthread_exit((void *) 1);
}

Would the main thread, by default, wait until thread1 or thread2 send it a signal or would we need some sort of a conditional loop around the wait? 默认情况下,主线程会等到线程1或线程2向其发送信号之前,还是需要围绕等待进行某种条件循环?

Also, how would the main thread have access to the exit status of the thread that signaled without explicitly calling pthread_join? 另外,主线程如何在不显式调用pthread_join的情况下访问发出信号的线程的退出状态? Or, is there a way to get the thread_id of the thread that signaled so that the main thread may join it to retrieve its exit status? 或者,是否有一种方法可以获取发出信号的线程的thread_id,以便主线程可以加入该线程以检索其退出状态?

If both threads run to completion before the main thread reaches the pthread_cond_wait() , then it will wait forever. 如果两个线程在主线程到达pthread_cond_wait()之前都运行完毕,那么它将永远等待。 Otherwise, the main thread will wait until one of the other threads signals the condition. 否则,主线程将等待,直到其他线程之一发出该状态信号为止。

No, you cannot ask the condition who signalled it. 不,您不能问谁发出了信号。

Your pthread condition has no memory; 您的pthread条件没有内存。 if no thread is waiting on the condition when it is signalled, the signal is not remembered. 如果在发出信号时没有线程正在等待该条件,则不会记住该信号。 What matters is the state you manage, protected by the mutex. 重要的是您所管理的状态,该状态受互斥量保护。 The pthread condition is simply the mechanism which allows the thread to wait if the state requires it. pthread条件只是机制,该机制允许线程在状态需要时等待。

So, whatever information you need to pass from the child threads to the parent, the trick is to do that under the mutex. 因此,无论需要从子线程传递到父线程的任何信息,诀窍都是在互斥量下进行。 In this case you want to pass the fact that the child has finished. 在这种情况下,您想传递孩子已经完成的事实。 Perhaps a simple bool , so the main thread: 也许是一个简单的bool ,所以主线程是:

  pthread_mutex_lock(&mutex) ;
  while (!t1_done && !t2_done)
    pthread_cond_wait(&cond, &mutex) ;
  pthread_mutex_unlock(&mutex) ;

And thread the first: 然后线程第一个:

  pthread_mutex_lock(&mutex) ;
  t1_done = true ;
  pthread_cond_signal(&cond) ;
  pthread_mutex_unlock(&mutex) ;

...all pretty straightforward, really. ...真的非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM