1.demo2.c 主要解决内存满了问题和读写速度不一致问题即读的比写的快。
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include "demo2.h"
#define MAX_BUFFER_NUM 5
#define MAX_DATA_LENGTH 1024
typedef struct {
char abyBuf[MAX_DATA_LENGTH];
int nBufferldx;
int nBufferEnable;
int nWriteBufferLength;
int nReadBufferLength;
int bWait;
pthread_mutex_t tBufferMutex;
pthread_cond_t tBufferReadCond;
}TBufferlnf;
static TBufferlnf g_tBufferInf[MAX_BUFFER_NUM];
void show_Buffer_Inf(void **pptHandle)
{
TBufferlnf *ptHandle = (TBufferlnf *)*pptHandle;
printf("nIdx=%d\n", ptHandle->nBufferldx);
printf("nEnable=%d\n", ptHandle->nBufferEnable);
printf("data nReadBufferLength=%d\n", ptHandle->nReadBufferLength);
printf("data nWriteBufferLength=%d\n", ptHandle->nWriteBufferLength);
}
int get_Data_From_Buffer(void **pptHandle, void *data,
int nDataLength)
{
TBufferlnf *ptHandle = (TBufferlnf *)*pptHandle;
int nReadLength;
int nNowDataLocation;
int nDiff;
if ((pptHandle == NULL) || (data == NULL))
{
printf("input param error, ptHandle=%p, data=%p\n", ptHandle, data);
return -1;
}
if (nDataLength >= MAX_DATA_LENGTH)
{
printf("input param error, nDataLength=%d", nDataLength);
return -2;
}
pthread_mutex_lock(&(ptHandle->tBufferMutex));
nDiff = ptHandle->nWriteBufferLength - ptHandle->nReadBufferLength;
if (nDiff > MAX_DATA_LENGTH)
{
printf(" nDiff error, nDiff=%d\n", nDiff);
return -3;
}
while (ptHandle->nWriteBufferLength < ptHandle->nReadBufferLength)
{
ptHandle->bWait = 1;
printf("waiting for product\n");
pthread_cond_wait(&(ptHandle->tBufferReadCond), &(ptHandle->tBufferMutex));
}
nNowDataLocation = (ptHandle->nReadBufferLength) % MAX_DATA_LENGTH;
ptHandle->nReadBufferLength += nDataLength;
if (ptHandle->nReadBufferLength >= MAX_DATA_LENGTH)
{
nReadLength = (ptHandle->nReadBufferLength) % MAX_DATA_LENGTH;
memcpy((char *)data, &(ptHandle->abyBuf[nNowDataLocation]), (MAX_DATA_LENGTH-nNowDataLocation));
memcpy(((char *)data + MAX_DATA_LENGTH - nNowDataLocation), &(ptHandle->abyBuf[0]),
nReadLength);
}
else
{
memcpy((char *)data, &(ptHandle->abyBuf[nNowDataLocation]), nDataLength);
}
pthread_mutex_unlock(&(ptHandle->tBufferMutex));
return 0;
}
int send_Data_To_Buffer(void **pptHandle, void *data,
int nDataLength)
{
TBufferlnf *ptHandle = (TBufferlnf *)*pptHandle;
int nNowDataLocation;
int nWriteLength;
if ((pptHandle == NULL) || (data == NULL))
{
printf("input param error, ptHandle=%p, data=%p\n", ptHandle,
data);
return -1;
}
if ((nDataLength <= 0) || (nDataLength >= MAX_DATA_LENGTH))
{
printf("input param error, nDataLength=%d\n", nDataLength);
return -2;
}
pthread_mutex_lock(&(ptHandle->tBufferMutex));
nNowDataLocation = (ptHandle->nWriteBufferLength) % MAX_DATA_LENGTH;
ptHandle->nWriteBufferLength += nDataLength;
if (ptHandle->nWriteBufferLength >= MAX_DATA_LENGTH)
{
nWriteLength = (ptHandle->nWriteBufferLength) % MAX_DATA_LENGTH;
memcpy(&(ptHandle->abyBuf[nNowDataLocation]), (char *)data, (MAX_DATA_LENGTH - nNowDataLocation));
memcpy(&(ptHandle->abyBuf[0]), ((char *)data + MAX_DATA_LENGTH - nNowDataLocation), nWriteLength);
}
else
{
memcpy(&(ptHandle->abyBuf[nNowDataLocation]), (char *)data, nDataLength);
}
if (ptHandle->bWait == 1)
{
if (ptHandle->nWriteBufferLength > ptHandle->nReadBufferLength)
{
printf("send signal to consumer\n");
ptHandle->bWait = 0;
pthread_cond_signal(&(ptHandle->tBufferReadCond));
}
}
pthread_mutex_unlock(&(ptHandle->tBufferMutex));
return 0;
}
int destory_Buffer(void **pptHandle)
{
if (*pptHandle == NULL)
{
return -1;
}
TBufferlnf *ptHandle = (TBufferlnf*)(*pptHandle);
ptHandle->nBufferEnable = 0;
return 0;
}
int create_Buffer(void **pptHandle)
{
int nIdx = 0;
*pptHandle = NULL;
for (nIdx = 0; nIdx < MAX_BUFFER_NUM; nIdx++)
{
pthread_mutex_lock(&(g_tBufferInf[nIdx].tBufferMutex));
if (!g_tBufferInf[nIdx].nBufferEnable)
{
*pptHandle = &g_tBufferInf[nIdx];
g_tBufferInf[nIdx].nBufferEnable = 1;
pthread_mutex_unlock(&(g_tBufferInf[nIdx].tBufferMutex));
break;
}
}
if (*pptHandle == NULL)
{
return -1;
}
return 0;
}
void buffer_Init()
{
int nIdx = 0;
memset(&g_tBufferInf[0], 0, sizeof(TBufferlnf)*MAX_BUFFER_NUM);
for (nIdx = 0; nIdx < MAX_BUFFER_NUM; nIdx++)
{
pthread_mutex_init(&(g_tBufferInf[nIdx].tBufferMutex), NULL);
pthread_cond_init(&(g_tBufferInf[nIdx].tBufferReadCond), NULL);
}
}
2.demo2.h
#ifndef _BUFFER_H
#define _BUFFER_H
void show_Buffer_Inf(void **pptHandle);
int get_Data_From_Buffer(void **pptHandle, void *data,
int nDataLength);
int send_Data_To_Buffer(void **pptHandle, void *data,
int nDataLength);
int destory_Buffer(void **pptHandle);
int create_Buffer(void **pptHandle);
void buffer_Init();
#endif
3.main.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "demo2.h"
void *pHandle = NULL;
void *WorkProduct(void *arg)
{
printf("This is product\n");
while(1)
{
send_Data_To_Buffer(&pHandle, "nixu", 4);
sleep(1);
}
return NULL;
}
void *WorkConsmue(void *arg)
{
printf("This is consume\n");
char readBuf[128];
while(1)
{
get_Data_From_Buffer(&pHandle, &readBuf[0], 3);
sleep(1);
printf("readBuf=%s\n", readBuf);
}
return NULL;
}
int main()
{
int nRet = 0;
pthread_t tCons;
pthread_t tProd;
buffer_Init();
nRet = create_Buffer(&pHandle);
if (nRet)
{
printf(" create_buffer error nRet=%d", nRet);
return -1;
}
pthread_create(&tCons, NULL, WorkProduct, NULL);
pthread_create(&tProd, NULL, WorkConsmue, NULL);
pthread_join(tCons, NULL);
pthread_join(tProd, NULL);
return 0;
}
2.编译时出现的报错
(1).||要加括号否则出现错误 2.GDB调试
|