linux下互斥锁实现的简单的生产者消费者问题1.0
这个程序实现的功能很简单,也算是入门linux下的多线程编程了吧~
其创造了两个生产者和一个消费者,两个生产者通过互斥锁实现同步,往缓冲区里放入数据,数据的值和其下标值一样,方便消费者的检验
消费者等到生产者放完数据后,从缓冲区中取出数据,并进行检验,看是否有出现差错,没有的话即实现了同步操作
/* include main */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAXNITEMS 10 //缓冲区大小为10
/*
两个生产者,实现它们之间的同步
缓冲区内第一个位置放1,第二个位置放2这种
一个消费者,检验其是否出错
*/
struct
{
pthread_mutex_t lock;
int key;
int value;
int buff[MAXNITEMS];
}shared = {
PTHREAD_MUTEX_INITIALIZER
};
//这样对结构体初始化
void * create_producer1()
{
for(;;)
{
pthread_mutex_lock(&shared.lock);
if(shared.key >= MAXNITEMS)
{
pthread_mutex_unlock(&shared.lock);
printf("producer1: my function is over~\n");
pthread_exit(NULL);
}
shared.buff[shared.key] = shared.value;
printf("producer1 have put the data: %d\n",shared.value);
shared.key ++;
shared.value ++;
pthread_mutex_unlock(&shared.lock);
}
}
void * create_producer2()
{
for(;;)
{
pthread_mutex_lock(&shared.lock);
if(shared.key >= MAXNITEMS)
{
pthread_mutex_unlock(&shared.lock);
printf("producer2: my function is over~\n");
pthread_exit(NULL);
}
shared.buff[shared.key] = shared.value;
printf("producer2 have put the data: %d\n",shared.value);
shared.key ++;
shared.value ++;
pthread_mutex_unlock(&shared.lock);
}
}
void * create_consumer()
{
for(int i = 0;i < 10;i++)
{
if(shared.buff[i] != i)
{
printf("buff[%d] = %d was wrong!\n", i , shared.buff[i]);
}
}
printf("All have scanned!\n");
return (NULL);
}
int main(int argc, char const *argv[])
{
pthread_t tid1,tid2;
pthread_t consumer_tid;
printf("I will create two producers and one consumers\n");
if(pthread_create(&tid1,NULL,create_producer1,NULL) != 0)
printf("create producer1 failed!\n");
else
printf("producer1 has been create!\n");
if(pthread_create(&tid2,NULL,create_producer2,NULL) != 0)
printf("create producer2 failed!\n");
else
printf("producer2 has been create!\n");
//等待生产者线程结束
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
if(pthread_create(&consumer_tid,NULL,create_consumer,NULL) != 0)
printf("create consumer failed!\n");
else
printf("consumer has been create and begin check out the buff...\n");
pthread_join(consumer_tid,NULL);
return 0;
}
运行截图: