- 论坛徽章:
- 0
|
生产者,2个线程
消费者,2个线程
想通过buffer把string“Hello World”的字符打印到stdout,现程序compile通过,可运行没结果,请大家帮忙看看。
- /*
- The size of buffer is more than one. Multiple producers, multiple consumers
- */
- #include <stdio.h>;
- #include <pthread.h>;
-
- #define N_PRODUCERS 2 /* the number of producers */
- #define N_CONSUMERS 2 /* the number of consumers */
- #define BUFFER_SIZE 3 /* the size of the buffer */
-
- int count = 0; /* the number of data items in the buffer */
- char buf[BUFFER_SIZE]; /* the buffer (circular buffer) */
- int head = 0; /* index of the head data in the buffer */
- int tail = 0; /* index of the tail data in the buffer */
- pthread_cond_t empty, full;
- pthread_mutex_t buf_mutex_lock, produced_lock, consumed_lock;
- char *message = "Hello_world.";
-
- void get_from_buf(char *ch)
- {
- while (count == 0)
- {
- /* Wait until producer adds something */
- pthread_cond_wait(&full, &consumed_lock);
- }
- pthread_mutex_lock(&buf_mutex_lock);
- *ch = buf[head];
- head = (head+1) % BUFFER_SIZE;
- count--;
- pthread_mutex_unlock(&buf_mutex_lock);
- pthread_cond_signal(&empty);
- }
- void put_to_buf(char *ch)
- {
- char *p = message;
-
- while (count == BUFFER_SIZE)
- {
- /* Wait until a consumer removes something. */
- pthread_cond_wait(&empty, &produced_lock);
- }
- pthread_mutex_lock(&buf_mutex_lock);
- buf[tail] = *ch = *(p++);
- tail = (tail+1) % BUFFER_SIZE;
- count++;
- pthread_mutex_unlock(&buf_mutex_lock);
- pthread_cond_signal(&full);
- }
- /*****************************************************************
- Producer
- *****************************************************************/
- void producer(void)
- {
- char ch;
-
- do
- {
- pthread_mutex_lock(&produced_lock);
- put_to_buf(&ch);
- pthread_mutex_unlock(&produced_lock);
- } while (ch != '\0');
- pthread_exit(NULL);
- }
- /*****************************************************************
- Consumer
- *****************************************************************/
- void consumer(void)
- {
- char ch;
-
- do
- {
- pthread_mutex_lock(&consumed_lock);
- get_from_buf(&ch);
- if (ch != '\0') /* print the character */
- printf("%c ", ch);
- pthread_mutex_unlock(&consumed_lock);
- } while (ch != '\0');
- pthread_exit(NULL);
- }
- int main(int argc, char **argv)
- {
- int i;
-
- pthread_t producer_threads[N_PRODUCERS];
- pthread_t consumer_threads[N_CONSUMERS];
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_mutex_init(&buf_mutex_lock, NULL);
- pthread_mutex_init(&produced_lock, NULL);
- pthread_mutex_init(&consumed_lock, NULL);
- pthread_cond_init(&empty, NULL);
- pthread_cond_init(&full, NULL);
- /* Create producers. */
- for (i = 0; i < N_PRODUCERS; i++)
- {
- if (pthread_create(&producer_threads[i], &attr, (void*) &producer, NULL) != 0)
- {
- /* Error. */
- }
- }
- /* Create consumers. */
- for (i = 0; i < N_CONSUMERS; i++)
- {
- if (pthread_create(&consumer_threads[i], &attr, (void*)&consumer, NULL) != 0)
- {
- /* Error. */
- }
- }
- /* Wait for them to finish. */
- for (i = 0; i < N_CONSUMERS; i++)
- {
- pthread_join(producer_threads[i], NULL);
- pthread_join(consumer_threads[i], NULL);
- }
- /* We're done. */
- return 0;
- }
复制代码 |
|