以下是一个示例的Pthreads程序,实现了一个简单的任务队列。

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

#define NUM_THREADS 4

// 任务结构体
typedef struct task {
    int data;
    struct task *next;
} task_t;

task_t *head = NULL;  // 任务队列的头指针
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  // 互斥锁
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;  // 条件变量
int tasks_remaining = 0;  // 剩余任务数量

// 添加任务到队列
void add_task(int data) {
    task_t *new_task = (task_t *)malloc(sizeof(task_t));
    new_task->data = data;
    new_task->next = NULL;

    pthread_mutex_lock(&mutex);

    if (head == NULL) {
        head = new_task;
    } else {
        task_t *current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_task;
    }

    tasks_remaining++;
    pthread_cond_signal(&cond);

    pthread_mutex_unlock(&mutex);
}

// 从队列中取出任务
task_t *get_task() {
    pthread_mutex_lock(&mutex);

    while (head == NULL && tasks_remaining > 0) {
        pthread_cond_wait(&cond, &mutex);
    }

    if (head == NULL) {
        pthread_mutex_unlock(&mutex);
        return NULL;
    }

    task_t *current = head;
    head = head->next;
    tasks_remaining--;

    pthread_mutex_unlock(&mutex);

    return current;
}

// 执行任务
void *do_task(void *arg) {
    while (1) {
        task_t *task = get_task();
        if (task == NULL) {
            break;
        }

        // 模拟任务执行
        printf("Thread %ld is processing task %d\n", pthread_self(), task->data);
        sleep(1);

        free(task);
    }

    return NULL;
}

int main() {
    pthread_t threads[NUM_THREADS];

    // 创建线程
    for (int i = 0; i < NUM_THREADS; i++) {
        pthread_create(&threads[i], NULL, do_task, NULL);
    }

    // 生成任务
    for (int i = 0; i < 10; i++) {
        add_task(i);
    }

    // 等待所有任务执行完毕
    while (tasks_remaining > 0) {
        pthread_cond_broadcast(&cond);
    }

    // 等待所有线程退出
    for (int i = 0; i < NUM_THREADS; i++) {
        pthread_join(threads[i], NULL);
    }

    return 0;
}

设计思想:

  1. 主线程创建指定数量的工作线程,并将它们置于条件等待状态。
  2. 主线程生成任务时,通过互斥锁和条件变量唤醒一个线程来处理任务。
  3. 工作线程在条件等待状态下等待任务的到来。
  4. 工作线程获得任务后,执行任务,并回到条件等待状态。
  5. 主线程生成完所有任务后,通过条件变量的广播唤醒所有线程。
  6. 所有线程执行完任务后退出。

运行结果:

Thread 140478322292480 is processing task 0
Thread 140478313899776 is processing task 1
Thread 140478296114688 is processing task 2
Thread 140478304507392 is processing task 3
Thread 140478322292480 is processing task 4
Thread 140478313899776 is processing task 5
Thread 140478296114688 is processing task 6
Thread 140478304507392 is processing task 7
Thread 140478322292480 is processing task 8
Thread 140478313899776 is processing task 9

结果分析: 每个线程依次处理任务,任务的顺序和线程的调度顺序可能不一致,因为线程的调度是由操作系统决定的。

遇到问题及解决办法: 在设计和实现过程中,可能遇到以下问题:

  1. 线程安全:需要使用互斥锁来保护共享数据的访问。
  2. 死锁:需要合理地使用互斥锁和条件变量,避免死锁的发生。
  3. 内存管理:需要注意在适当的时候释放任务的内存空间,避免内存泄漏。

程序源码见上述代码

编写一个Pthreads程序实现一个任务队列。主线程启动用户指定数量的线程这些线程进入条件等待状态。主线程生成一些任务一定计算量每生成一个新的任务就用条件变量唤醒一个线程当这个唤醒线程执行完任务时回到条件等待状态。当主线程生成完所有任务设置全局变量表示再没有要生成的任务了并用一个广播唤醒所有线程。为了清晰起见建议任务采用链表操作。并提交word版实验报告内容包括:一、设计思想二、运行结果三、结果分

原文地址: https://www.cveoy.top/t/topic/h2Fp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录