简体   繁体   English

C - 如何在其中一个线程用条件变量向主线程发出信号后终止所有线程?

[英]C - How to terminate all threads after one of them signals the main thread with a condition variable?

In my program the main thread starts multiple child threads, locks a mutex for a counter and then sleeps until the counter reaches N.在我的程序中,主线程启动多个子线程,为计数器锁定一个互斥锁,然后休眠直到计数器达到 N。

In an infinite for loop, the other threads do some work and they increase the counter in each iteration alternately.在无限循环中,其他线程做一些工作,它们在每次迭代中交替增加计数器。 Once the counter == N one of them has to signal the main thread and all of the child threads have to end.一旦计数器 == N,其中一个必须向主线程发出信号,并且所有子线程都必须结束。

My problem is that I'm not sure when I should lock the counter mutex and make a signal in the thread function, because once the counter becomes NI managed to wake up the main thread and exit one of the child threads, but the other threads will keep on trying to work when they should all be terminating.我的问题是我不确定何时应该锁定计数器互斥锁并在线程函数中发出信号,因为一旦计数器变为 NI 设法唤醒主线程并退出一个子线程,但其他线程当他们都应该终止时,将继续努力工作。

My question is, how can I check if counter == N but send a signal by only one of the threads, and the others just return without any signalling?我的问题是,如何检查 counter == N 是否仅通过其中一个线程发送信号,而其他线程仅在没有任何信号的情况下返回? Can this be done by only locking the mutex once in each iteration, for the time of checking its value (== N)?这是否可以通过在每次迭代中只锁定互斥锁一次来完成,以便检查其值 (== N)? If yes, how?如果是,如何?

void *thread_function(void *arg) {
    int *id = (int *) arg;

    for (;;) {
        pthread_mutex_lock(&mutex_counter);
        counter++;
        if (counter == N) {
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&mutex_counter);
            return NULL;
        }
        if (counter > N) {
            pthread_mutex_unlock(&mutex_counter);
            return NULL;
        }
        pthread_mutex_unlock(&mutex_counter);
        
        sleep(random());
        // do some work here
        // ...
        // finish work
    }

The problem with above code is that, despite all threads terminating, they execute the loop one less iteration than needed because the counter is increased before the if clause (so counter will be N, but there were no N loops of work done).上面代码的问题在于,尽管所有线程都终止了,但它们执行的循环比所需的迭代次数少了一次,因为计数器在 if 子句之前增加了(因此计数器将为 N,但没有完成 N 次工作循环)。

However, if I put the counter to the end of the function, the threads will do more work than needed which messes up my program.但是,如果我将计数器放在函数的末尾,线程将做比需要更多的工作,这会弄乱我的程序。

why you lock counter-mutex in main thread?为什么在主线程中锁定反互斥锁? Why you need to send signal from other treads to main?为什么需要从其他踏板向主踏板发送信号? If you have only one global counter and all threads increasing it, you can just start in main thread all threads, remember their thread ids and then wait using while(counter<N);如果您只有一个全局计数器并且所有线程都在增加它,那么您可以在主线程中启动所有线程,记住它们的线程 ID,然后使用 while(counter<N); 等待。 instruction.操作说明。 After all, just kill all threads with pthread_cancel.毕竟,只需使用 pthread_cancel 杀死所有线程。

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

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