c - 缓慢的 pthread 消费者

标签 c multithreading pthreads producer-consumer

我使用 pthreads 和信号量在 C 中实现了生产者/消费者问题的解决方案。

我的主线程是生产者,我启动 N 个消费者线程。

我的代码是:

typedef struct
{
    int buf[BUFSIZE];     /* shared var */
    int in;               /* buf[in%BUFSIZE] is the first empty slot */
    int out;              /* buf[out%BUFSIZE] is the first full slot */
    sem_t full;           /* keep track of the number of full spots */
    sem_t empty;          /* keep track of the number of empty spots */
    pthread_mutex_t mutex;          /* enforce mutual exclusion to shared data */
} CONSUMER_STRUCT;

CONSUMER_STRUCT shared;

这是我的每个消费者线程的代码:

void *Consumer(void *arg)
{
    int fd, workerID, i, hit=0;

    workerID = *(int *)arg;

    for (;;) {
        sem_wait(&shared.full);
        pthread_mutex_lock(&shared.mutex);
        fd = shared.buf[shared.out];
        printf("\n[C%d] Consumed. I got  %d ...Valor do buffer: %d na posição %d\n\n\n", workerID, fd, shared.buf[shared.out], shared.out);
        ftp(fd, hit);
        shared.buf[shared.out] = 0;
        shared.out = (shared.out+1)%BUFSIZE;
        fflush(stdout);
        printf("\n\n\n\nEstado do buffer:\n\n\n\n");
        for (i = 0; i < BUFSIZE; i++) {
            //printf("%d ", shared.buf[i]);
        }
        /* Release the buffer */
        pthread_mutex_unlock(&shared.mutex);
        /* Increment the number of full slots */
        sem_post(&shared.empty);
        hit++;
    }
    return NULL;
}

这是我的生产者线程的代码:

item = socketfd;

sem_wait(&shared.empty);
pthread_mutex_lock(&shared.mutex);

shared.buf[shared.in] = item;

shared.in = (shared.in + 1) % BUFSIZE;
fflush(stdout);

pthread_mutex_unlock(&shared.mutex);
sem_post(&shared.full);

一切正常,但提供 22 个文件大约需要 20 秒,而每个请求创建一个线程大约需要 2 秒!这似乎一次执行一个线程,我想“同时”执行所有线程。

我的实现方法是否做错了什么?

最佳答案

对于那些可能遇到类似问题的人,这里是解决方法。

感谢@Martin James 和@EOF。

void *Consumer(void *arg)
{
    int fd, workerID, i, hit=0;

    workerID = *(int *)arg;

    for (;;) {
        sem_wait(&shared.full);
        pthread_mutex_lock(&shared.mutex);
        fd = shared.buf[shared.out];
        shared.buf[shared.out] = 0;
        shared.out = (shared.out+1)%BUFSIZE;
        pthread_mutex_unlock(&shared.mutex);
        printf("\n[C%d] Consumed. I got  %d ...Valor do buffer: %d na posição %d\n\n\n", workerID, fd, shared.buf[shared.out], shared.out);
        ftp(fd, hit);
        fflush(stdout);
        printf("\n\n\n\nEstado do buffer:\n\n\n\n");
        for (i = 0; i < BUFSIZE; i++) {
            //printf("%d ", shared.buf[i]);
        }
        /* Release the buffer */
        /* Increment the number of full slots */
        sem_post(&shared.empty);
        hit++;
    }
    return NULL;
}

问题是我锁定了互斥锁,执行了一个函数,然后解锁了互斥锁。这就是导致执行延迟如此之多的原因。

关于c - 缓慢的 pthread 消费者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36723599/

相关文章:

c - 开关盒内循环

c - 为什么在写入使用字符串初始化的 "char *s"时会出现段错误,而不是 "char s[]"?

c - 当 abcde 既是类型又是指针时,C 中的 sizeof(abcde)

c# - 如何杀死 C# 线程?

python - 多处理池工作线程中的线程标识符

c - 我使用 pthread 创建了一个矩阵乘法程序但是 [0][0] block 值总是 0

c - 使用linux套接字通信(通过wifi)时,Socket-CAN不起作用

c++ - 在 C++ 中使用 vector 的多线程

c - 从不同的线程通过 UDP 进行多播

c++ - 线程 vector 中的假缓存共享 C++