生产者消费者永不终止

发布于 2025-02-01 08:25:23 字数 2905 浏览 2 评论 0原文

我正在使用多线程,并且制作了一个程序,生产者创建价值并插入缓冲区,消费者将其消耗掉。 但是,消费者永远不会终止。我想终止消费者流程,当时没有食用。

有什么方法可以更改代码?

这是主要代码,

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

/* Producer/consumer program illustrating conditional variables */

/* Size of shared buffer */
#define BUF_SIZE 3

int buffer[BUF_SIZE];                           /* shared buffer */
int add=0;                                      /* place to add next element */
int rem=0;                                      /* place to remove next element */
int num=0;                                      /* number elements in buffer */
pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;    /* mutex lock for buffer */
pthread_cond_t c_cons=PTHREAD_COND_INITIALIZER; /* consumer waits on this cond var */
pthread_cond_t c_prod=PTHREAD_COND_INITIALIZER; /* producer waits on this cond var */

void *producer(void *param);
void *consumer(void *param);

main (int argc, char *argv[])
{
    pthread_t tid1, tid2;       /* thread identifiers */
    int i;

    /* create the threads; may be any number, in general */
    if (pthread_create(&tid1,NULL,producer,NULL) != 0) {
        fprintf (stderr, "Unable to create producer thread\n");
        exit (1);
    }
    if (pthread_create(&tid2,NULL,consumer,NULL) != 0) {
        fprintf (stderr, "Unable to create consumer thread\n");
        exit (1);
    }
    /* wait for created thread to exit */
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    printf ("Parent quiting\n");
}

这是生产者部分



/* Produce value(s) */
void *producer(void *param)
{
    int i;
    for (i=1; i<=20; i++) {
        /* Insert into buffer */
        pthread_mutex_lock (&m);
        if (num > BUF_SIZE) exit(1);    /* overflow */
        while (num == BUF_SIZE)         /* block if buffer is full */
            pthread_cond_wait (&c_prod, &m);
        /* if executing here, buffer not full so add element */
        buffer[add] = i;
        add = (add+1) % BUF_SIZE;
        num++;
        pthread_mutex_unlock (&m);
        pthread_cond_signal (&c_cons);
        printf ("producer: inserted %d\n", i);  fflush (stdout);
    }
    printf ("producer quiting\n");  fflush (stdout);
}

,这是消费者部分


/* Consume value(s); Note the consumer never terminates */
void *consumer(void *param)
{
    int i;
    while (1) {
        pthread_mutex_lock (&m);
        if (num < 0) exit(1);   /* underflow */
        while (num == 0)        /* block if buffer empty */
            pthread_cond_wait (&c_cons, &m);
        /* if executing here, buffer not empty so remove element */
        i = buffer[rem];
        rem = (rem+1) % BUF_SIZE;
        num--;
        pthread_mutex_unlock (&m);
        pthread_cond_signal (&c_prod);
        printf ("Consume value %d\n", i);  fflush(stdout);
    }
}

I am using multi thread and I have made a program where producer creates values and insert into buffer and consumer consumes it.
However consumer never terminates. I would like to terminate the consumer process when there is nothing left to consume.

Is there any way to change the code?

This is the main code

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

/* Producer/consumer program illustrating conditional variables */

/* Size of shared buffer */
#define BUF_SIZE 3

int buffer[BUF_SIZE];                           /* shared buffer */
int add=0;                                      /* place to add next element */
int rem=0;                                      /* place to remove next element */
int num=0;                                      /* number elements in buffer */
pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;    /* mutex lock for buffer */
pthread_cond_t c_cons=PTHREAD_COND_INITIALIZER; /* consumer waits on this cond var */
pthread_cond_t c_prod=PTHREAD_COND_INITIALIZER; /* producer waits on this cond var */

void *producer(void *param);
void *consumer(void *param);

main (int argc, char *argv[])
{
    pthread_t tid1, tid2;       /* thread identifiers */
    int i;

    /* create the threads; may be any number, in general */
    if (pthread_create(&tid1,NULL,producer,NULL) != 0) {
        fprintf (stderr, "Unable to create producer thread\n");
        exit (1);
    }
    if (pthread_create(&tid2,NULL,consumer,NULL) != 0) {
        fprintf (stderr, "Unable to create consumer thread\n");
        exit (1);
    }
    /* wait for created thread to exit */
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    printf ("Parent quiting\n");
}

This is the producer part



/* Produce value(s) */
void *producer(void *param)
{
    int i;
    for (i=1; i<=20; i++) {
        /* Insert into buffer */
        pthread_mutex_lock (&m);
        if (num > BUF_SIZE) exit(1);    /* overflow */
        while (num == BUF_SIZE)         /* block if buffer is full */
            pthread_cond_wait (&c_prod, &m);
        /* if executing here, buffer not full so add element */
        buffer[add] = i;
        add = (add+1) % BUF_SIZE;
        num++;
        pthread_mutex_unlock (&m);
        pthread_cond_signal (&c_cons);
        printf ("producer: inserted %d\n", i);  fflush (stdout);
    }
    printf ("producer quiting\n");  fflush (stdout);
}

This is the consumer part


/* Consume value(s); Note the consumer never terminates */
void *consumer(void *param)
{
    int i;
    while (1) {
        pthread_mutex_lock (&m);
        if (num < 0) exit(1);   /* underflow */
        while (num == 0)        /* block if buffer empty */
            pthread_cond_wait (&c_cons, &m);
        /* if executing here, buffer not empty so remove element */
        i = buffer[rem];
        rem = (rem+1) % BUF_SIZE;
        num--;
        pthread_mutex_unlock (&m);
        pthread_cond_signal (&c_prod);
        printf ("Consume value %d\n", i);  fflush(stdout);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2025-02-08 08:25:23

通常的方法是让生产商在缓冲区中放一些东西,以告诉消费者退出。由于您的数字从1到20,因此您可以选择0或A -1表示结束。当生产者结束时,它将-1在缓冲区中,当消费者在缓冲区中看到-1时,它也会结束。

The usual way to do this is for the producer to put something in the buffer that tells the consumer it's quitting. Since your numbers go from 1 to 20, you could choose that a 0 or a -1 means the end. When the producer ends it puts -1 in the buffer and when the consumer sees a -1 in the buffer it also ends.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文