多个生产者,单个消费者,只考虑生产者之间的同步互斥
#include "unpipc.h"

#define MAXNITEMS 1000000
#define MAXNTHREADS 100


int nitems; /**//* read-only by producer and consumer */

struct
{
pthread_mutex_t mutex; //互斥信号量
int buff[MAXNITEMS];
int nput;//下一个存放元素的下标
int nval;//下一个存放的值

} shared =
{ PTHREAD_MUTEX_INITIALIZER };

void *produce(void *), *consume(void *);

int
main(int argc, char **argv)


{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;

if (argc != 3)
err_quit("usage: prodcons2 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);//生产者存放的条目数
nthreads = min(atoi(argv[2]), MAXNTHREADS);//待创建的生产者线程数

Set_concurrency(nthreads);//通知线程系统我们希望并发多少线程

/**//* 4start all the producer threads */

for (i = 0; i < nthreads; i++)
{
count[i] = 0;//计数器初始化为0
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);//创建生产者线程
}


/**//* 4wait for all the producer threads */

for (i = 0; i < nthreads; i++)
{
Pthread_join(tid_produce[i], NULL);//等待生产者线程结束
printf("count[%d] = %d\n", i, count[i]);
}
//这样做的目的是为了避免生产者和消费者之间的同步问题,这里只考虑生产者之间的同步

/**//* 4start, then wait for the consumer thread */
Pthread_create(&tid_consume, NULL, consume, NULL);//创建消费者线程
Pthread_join(tid_consume, NULL);

exit(0);
}

/**//* end main */


/**//* include producer */
void *
produce(void *arg)


{

for ( ; ; )
{
Pthread_mutex_lock(&shared.mutex);

if (shared.nput >= nitems)
{//生产完毕
Pthread_mutex_unlock(&shared.mutex);

return(NULL); /**//* array is full, we're done */
}
shared.buff[shared.nput] = shared.nval;
shared.nput++;
shared.nval++;
Pthread_mutex_unlock(&shared.mutex);
*((int *) arg) += 1;//每个线程私有的计数器,不需要加锁
}
}

void *
consume(void *arg)


{
int i;


for (i = 0; i < nitems; i++)
{
if (shared.buff[i] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}
return(NULL);
}

/**//* end producer */

生产者和消费者之间的同步,消费者采用的方式就是不断轮询。

#include "unpipc.h"

#define MAXNITEMS 1000000
#define MAXNTHREADS 100


int nitems; /**//* read-only by producer and consumer */

struct
{
pthread_mutex_t mutex;
int buff[MAXNITEMS];
int nput;
int nval;

} shared =
{ PTHREAD_MUTEX_INITIALIZER };

void *produce(void *), *consume(void *);


/**//* include main */
int
main(int argc, char **argv)


{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;

if (argc != 3)
err_quit("usage: prodcons3 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS);


/**//* 4create all producers and one consumer */
Set_concurrency(nthreads + 1);

for (i = 0; i < nthreads; i++)
{
count[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
Pthread_create(&tid_consume, NULL, consume, NULL);


/**//* 4wait for all producers and the consumer */

for (i = 0; i < nthreads; i++)
{
Pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
Pthread_join(tid_consume, NULL);

exit(0);
}

/**//* end main */

void *
produce(void *arg)


{

for ( ; ; )
{
Pthread_mutex_lock(&shared.mutex);

if (shared.nput >= nitems)
{
Pthread_mutex_unlock(&shared.mutex);

return(NULL); /**//* array is full, we're done */
}
shared.buff[shared.nput] = shared.nval;
shared.nput++;
shared.nval++;
Pthread_mutex_unlock(&shared.mutex);
*((int *) arg) += 1;
}
}


/**//* include consume */
void
consume_wait(int i)


{

for ( ; ; )
{
Pthread_mutex_lock(&shared.mutex);

if (i < shared.nput)
{
Pthread_mutex_unlock(&shared.mutex);

return; /**//* an item is ready */
}
Pthread_mutex_unlock(&shared.mutex);
}
}

void *
consume(void *arg)


{
int i;


for (i = 0; i < nitems; i++)
{
consume_wait(i);
if (shared.buff[i] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}
return(NULL);
}

/**//* end consume */

每个条件变量都有一个互斥锁与之关联,调用pthread_cond_wait等待某个条件为真时,同时指定其条件变量的地址和所关联的互斥锁的地址
使用条件变量来通知,避免消费者轮询

/**//* include globals */
#include "unpipc.h"

#define MAXNITEMS 1000000
#define MAXNTHREADS 100


/**//* globals shared by threads */

int nitems; /**//* read-only by producer and consumer */
int buff[MAXNITEMS];

struct
{//用于生产者之间的同步
pthread_mutex_t mutex;

int nput; /**//* next index to store */

int nval; /**//* next value to store */

} put =
{ PTHREAD_MUTEX_INITIALIZER };


struct
{//用于生产者和消费者之间同步
pthread_mutex_t mutex;
pthread_cond_t cond;

int nready; /**//* number ready for consumer */

} nready =
{ PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER };

/**//* end globals */

void *produce(void *), *consume(void *);


/**//* include main */
int
main(int argc, char **argv)


{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;

if (argc != 3)
err_quit("usage: prodcons6 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS);

Set_concurrency(nthreads + 1);

/**//* 4create all producers and one consumer */

for (i = 0; i < nthreads; i++)
{
count[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
Pthread_create(&tid_consume, NULL, consume, NULL);


/**//* wait for all producers and the consumer */

for (i = 0; i < nthreads; i++)
{
Pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
Pthread_join(tid_consume, NULL);

exit(0);
}

/**//* end main */


/**//* include prodcons */
void *
produce(void *arg)


{

for ( ; ; )
{
Pthread_mutex_lock(&put.mutex);

if (put.nput >= nitems)
{
Pthread_mutex_unlock(&put.mutex);

return(NULL); /**//* array is full, we're done */
}
buff[put.nput] = put.nval;
put.nput++;
put.nval++;
Pthread_mutex_unlock(&put.mutex);

Pthread_mutex_lock(&nready.mutex);
if (nready.nready == 0)
Pthread_cond_signal(&nready.cond);//发出信号唤醒消费者
nready.nready++;//置为1
Pthread_mutex_unlock(&nready.mutex);

*((int *) arg) += 1;
}
}

void *
consume(void *arg)


{
int i;


for (i = 0; i < nitems; i++)
{
Pthread_mutex_lock(&nready.mutex);
while (nready.nready == 0)
Pthread_cond_wait(&nready.cond, &nready.mutex);//wait条件变量
nready.nready--;//置为0
Pthread_mutex_unlock(&nready.mutex);

if (buff[i] != i)
printf("buff[%d] = %d\n", i, buff[i]);
}
return(NULL);
}

/**//* end prodcons */

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
2006-05-28 《Java编程思想》读书笔记(3)