等待线程池中所有任务完成的问题

问题描述 投票:0回答:0

问题: 我正在使用 pthreads 在 C 中实现一个线程池,并且我面临着等待所有任务完成然后再继续程序的其余部分的问题。线程池似乎工作正常,并且任务正在执行,但是当我尝试使用条件等待等待所有任务完成时,程序陷入无限循环。

说明: 我有一个具有固定数量线程(NUMBER_OF_THREADS)的线程池。线程池使用init_thread_pool()进行初始化,每个线程运行run_thread函数。任务由Task结构体表示,TaskQueue结构体用于管理任务队列。

我尝试使用 wait_thread_pool() 函数等待所有任务完成,该函数使用条件等待来等待,直到 task_queue->active_tasks 变为零。但是,条件等待似乎没有正确发出信号,并且程序陷入 wait_thread_pool() 内的 while 循环中。

我尝试使用互斥体和条件变量(tasks_done_lock 和tasks_done_cond)来同步等待,但它似乎没有按预期工作。

代码片段:

`
// Relevant code snippets from thread_pool.c and main.c

// thread_pool.c


void* run_thread(void* args) {
    Task* task = NULL;
    while(is_running == 1) {    
        pthread_mutex_lock(&queue_lock);
        while(task_queue->element_count == 0){
            pthread_cond_wait(&tasks_available_cond, &queue_lock);
        }
        task = pop_task();
        task_queue->element_count --;
        pthread_mutex_unlock(&queue_lock);

        task->function(task->data);

        pthread_mutex_lock(&tasks_done_lock);
        task_queue->active_tasks++;
        if(task_queue->active_tasks == 0) {
            pthread_cond_signal(&tasks_done_cond);
        }
        pthread_mutex_unlock(&tasks_done_lock);
    }
}


void wait_thread_pool() {
    pthread_mutex_lock(&tasks_done_lock);
    while (task_queue->active_tasks > 0) {
        pthread_cond_wait(&tasks_done_cond, &tasks_done_lock);
    }
    pthread_mutex_unlock(&tasks_done_lock);
}


void clean_thread_pool() {

    is_running = 0;
    for(int i = 0; i < NUMBER_OF_THREADS; i++) {
        pthread_join(threads[i], NULL);
    }

    free(threads); 

    pthread_mutex_destroy(&queue_lock);
    pthread_cond_destroy(&tasks_available_cond);

    pthread_mutex_destroy(&tasks_done_lock);
    pthread_cond_destroy(&tasks_done_cond);

    free(task_queue->tasks);
    free(task_queue);
}

void clean_thread_pool() {

    is_running = 0;
    for(int i = 0; i < NUMBER_OF_THREADS; i++) {
        pthread_join(threads[i], NULL);
    }

    free(threads); 

    pthread_mutex_destroy(&queue_lock);
    pthread_cond_destroy(&tasks_available_cond);

    pthread_mutex_destroy(&tasks_done_lock);
    pthread_cond_destroy(&tasks_done_cond);

    free(task_queue->tasks);
    free(task_queue);
}

// main.c

// one of the tasks I test with
void sum(void* args) {
    Data* data = (Data*) args;
    data->output = data->a + data->b;

    pthread_mutex_lock(&tasks_done_lock);
    task_queue->active_tasks -= 1;
    pthread_mutex_unlock(&tasks_done_lock);
}

int main() {
    // ... (other code)

    init_thread_pool();
    Data* data = (Data*)malloc(sizeof(Data));
    data->a = 4;
    data->b = 9;
    data->output = 0;
    data->operation = SUM;

    Task* task = (Task*)malloc(sizeof(Task));
    task->data = data;
    task->function = sum;

    push_task(task);

    wait_thread_pool();

    printf("thread finished, the output for the task is: %d \n", data->output);

    clean_thread_pool();
    return 0;
}`

预期行为: 我希望程序等到线程池中的所有任务都完成后才继续执行其余的代码。

实际行为: 当使用 wait_thread_pool() 函数等待所有任务完成时,程序陷入无限循环。

注意: 我使用条件变量和互斥体来同步等待,但它似乎没有按预期工作。我想知道我使用条件变量的方式是否存在问题,或者我是否在代码中遗漏了某些内容。任何有助于理解和解决此问题的帮助将不胜感激!

c pthreads threadpool
© www.soinside.com 2019 - 2024. All rights reserved.