5

我正在尝试设置几个线程处于等待状态,直到它们收到pthread_cond_broadcast().

完成一项工作后,我希望线程回到它们的等待状态。

我还希望调用 的进程pthread_cond_broadcast()在继续之前等待所有线程返回其等待状态。在这种情况下,调用广播的是 main() 函数。我正在尝试执行此操作 b 让 main(0pthread_cond_wait()在调用广播后执行 a 。

void* Work::job(void* id)
{
    int idx = (long)id;

    while(1)
    {
        pthread_mutex_lock(&job_lock);

        while(!jobs_complete)
        {
            // wait for main to broadcast
            pthread_cond_wait(&can_work, &job_lock);
            pthread_mutex_unlock(&job_lock);

            // work here

            pthread_mutex_lock(&job_lock);
            ++jobs_completed;

            if(jobs_completed == NUM_THREADS)
            {
                jobs_complete = true;
                pthread_cond_signal(&jobs_done);
                pthread_mutex_unlock(&job_lock);
            }
            pthread_mutex_unlock(&job_lock);
        }

        pthread_mutex_unlock(&job_lock);
    }

    return NULL;
}

NUM_THREADS是 4,job_lock是 a pthread_mutex_tcan_workjobs_donepthread_cond_tjobs_completed是 a booljobs_complete是 an int

// work

jobs_completed = false;
jobs_complete = 0;
pthread_mutex_lock(&job_lock);
pthread_cond_broadcast(&can_work);
pthread_cond_wait(&jobs_complete);
pthread_mutex_unlock(&job_lock);

// work that depends on jobs_complete

现在,我现在通过调用pthread_cond_broadcast()然后pthread_cond_wait()在它之后立即执行此操作,但这似乎陷入僵局。

谁能解释我应该怎么做或者我哪里出错了?我会很感激任何帮助。

谢谢!

4

2 回答 2

4

我只是发布这个(这几乎是所有 C 代码,但 pthreads 也是如此,所以请稍微松懈一下)来演示一种方法来做我认为你想要完成的事情。显然,您希望将其中的大部分正确地封装在适当的类等中。希望这将向您展示条件变量、互斥锁以及它们与谓词管理和通知的关系如何工作。

希望对你有帮助。祝你有美好的一天。

#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;

// our global condition variable and mutex
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

// our predicate values.
bool finished = false;
int jobs_waiting = 0;
int jobs_completed = 0;

// our thread proc
static void *worker_proc(void* p)
{
    intptr_t id = (intptr_t)p;  // our id
    size_t n_completed = 0;     // our job completion count

    // always latch prior to eval'ing predicate vars.
    pthread_mutex_lock(&mtx);
    while (!finished)
    {
        // wait for finish or work-waiting predicate
        while (!finished && jobs_waiting == 0)
            pthread_cond_wait(&cv, &mtx);

        // we own the mutex, so we're free to look at, modify
        //  etc. the values(s) that we're using for our predicate
        if (finished)
            break;

        // must be a job_waiting, reduce that number by one, then
        //  unlock the mutex and start our work. Note that we're
        //  changing the predicate (jobs_waiting is part of it) and
        //  we therefore need to let anyone that is monitoring know.
        --jobs_waiting;
        pthread_cond_broadcast(&cv);
        pthread_mutex_unlock(&mtx);

        // DO WORK HERE (this just runs a lame summation)
        for (int i=0,x=0;i<1048576; x += ++i);
        ++n_completed;

        // finished work latch mutex and setup changes
        pthread_mutex_lock(&mtx);
        ++jobs_completed;
        pthread_cond_broadcast(&cv);
    }

    // final report
    cout << id << ": jobs completed = " << n_completed << endl;

    // we always exit owning the mutex, so unlock it now. but
    //  let anyone else know they should be quitting as well.
    pthread_cond_broadcast(&cv);
    pthread_mutex_unlock(&mtx);
    return p;
}

// sets up a batch of work and waits for it to finish.
void run_batch(int num)
{
    pthread_mutex_lock(&mtx);
    jobs_waiting = num;
    jobs_completed = 0;
    pthread_cond_broadcast(&cv);

    // wait or all jobs to complete.
    while (jobs_completed != num)
        pthread_cond_wait(&cv, &mtx);

    // we own this coming out, so let it go.
    pthread_mutex_unlock(&mtx);
}

// main entry point.
int main()
{
    // number of threads in our crew
    static const size_t N = 7;
    pthread_t thrds[N] = {0};

    // startup thread crew.
    intptr_t id = 0;
    for (size_t i=0; i<N; ++i)
        pthread_create(thrds + i, NULL, worker_proc, (void*)(++id));

    // run through batches. each batch is one larger
    //  than the prior batch. this should result in some
    //  interesting job-counts per-thread.
    for (int i=0; i<64; ++i)
        run_batch(i);

    // flag for shutdown state.
    pthread_mutex_lock(&mtx);
    finished = true;
    pthread_cond_broadcast(&cv);
    pthread_mutex_unlock(&mtx);
    for (size_t i=0; i<N; pthread_join(thrds[i++], NULL));

    return 0;
}

样本输出 #1

3: jobs completed = 256
6: jobs completed = 282
5: jobs completed = 292
2: jobs completed = 242
1: jobs completed = 339
4: jobs completed = 260
7: jobs completed = 409

样本输出 #2

6: jobs completed = 882
1: jobs completed = 210
4: jobs completed = 179
5: jobs completed = 178
2: jobs completed = 187
7: jobs completed = 186
3: jobs completed = 194

样本输出#3

1: jobs completed = 268
6: jobs completed = 559
3: jobs completed = 279
5: jobs completed = 270
2: jobs completed = 164
4: jobs completed = 317
7: jobs completed = 159

固定批量大小

相同的代码,但改变了这一点:

for (int i=0; i<64; ++i)
    run_batch(i);

对此:

for (int i=0; i<64; ++i)
    run_batch(N);

给出以下内容,这可能更接近你真正想要的。

样本输出 #1

4: jobs completed = 65
2: jobs completed = 63
5: jobs completed = 66
3: jobs completed = 63
1: jobs completed = 64
7: jobs completed = 63
6: jobs completed = 64

样本输出 #2

3: jobs completed = 65
5: jobs completed = 62
1: jobs completed = 67
7: jobs completed = 63
2: jobs completed = 65
6: jobs completed = 61
4: jobs completed = 65

样本输出#3

2: jobs completed = 58
4: jobs completed = 61
5: jobs completed = 69
7: jobs completed = 68
3: jobs completed = 61
1: jobs completed = 64
6: jobs completed = 67
于 2013-03-28T09:36:17.323 回答
1

在函数结束时,您有 3 次可能的连续调用pthread_mutex_unlock,这将导致未定义的行为。你实际上不需要两个内部的。如果jobs_completetrue,线程将退出循环并释放锁,否则它将循环并需要它来等待can_work条件。

还有,还有

 pthread_cond_wait(&jobs_complete);

你可能的意思是:

pthread_cond_wait(&jobs_complete,&job_lock);

此外,该函数需要 apthread_cond_t *和 a pthread_mutex_t *,而不是 a int,因此即使那样,该代码也显然被破坏了。

请注意,条件变量上的信号或广播只会对已经在等待变量的线程产生影响。该信号不会保留以供将来等待。因此,当线程在 while 块上循环jobs_complete并再次等待时,必须再次向它们发出信号以恢复工作。

另一件事:您提到job_completeasintjob_completedas的类型bool,但您的代码似乎不同意:

        if(jobs_completed == NUM_THREADS)
        {
            jobs_complete = true;

这是我的建议:学习信号量和屏障抽象模型,如果可以的话,使用现有的实现(booststd在 C++11 中),或者使用pthreadAPI 重新实现它们。与操作 cond 变量相比,这些将帮助您更轻松地处理这种情况。在这个网站上寻找现有的解决方案。例如这个问题处理一个非常相似的问题,我提供的解决方案可以很容易地修改为使用 pthread API 来满足您的要求。

于 2013-03-28T07:48:12.100 回答