#include <cstring>
#include "global.h"
using std::cout;
using std::cerr;
using std::endl;
void *blkProducer(void *producerNum)
{
int count;
int *nproducers = (int *)producerNum;
for (;;)
{
//对互斥量bufferMutex上锁
pthread_mutex_lock(&bufferMutex);
while (bufferNum == 0)
//等 待
pthread_cond_wait(&bufferWriteCond, &bufferMutex);
if (count > (MAXBLOCK + *nproducers))
{
//对互斥量bufferMutex解锁
pthread_mutex_unlock(&bufferMutex);
break;
}
if ((temp = (buff_node *)(malloc(sizeof(buff_node)))) == NULL)
{
//对互斥量bufferMutex解锁
pthread_mutex_unlock(&bufferMutex);
continue;
}
//对temp->buff赋值
memset(temp->buff, '1', blockSize);
temp->node_id = count = ++blockIndex;
temp->next = head;
tail->next = temp;
tail = temp;
--bufferNum;
//释放信号量
pthread_cond_signal(&bufferReadCond);
//对互斥量bufferMutex解锁
pthread_mutex_unlock(&bufferMutex);
if (count <= MAXBLOCK)
{
//对互斥量stdoutMutex上锁
pthread_mutex_lock(&stdoutMutex);
cout << "post one block, block id is: " << count << endl;
//对互斥量stdoutMutex解锁
pthread_mutex_unlock(&stdoutMutex);
}
}
return NULL;
}
08-28
08-28
08-28
08-28