我制作了一个循环缓冲区,多个客户端写入(最后我希望他们将不同大小的消息)写入缓冲区。服务器将它们读出。它基于消费者/生产者问题中的代码:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#define BUFFER_SIZE 10
struct cBuf{
char *buf;
int size;
int start;
int end;
pthread_mutex_t mutex;
pthread_cond_t buffer_full;
pthread_cond_t buffer_empty;
};
struct cBuf cb;
void buf_Init(struct cBuf *cb, int size) {
int i;
cb->size = size + 1;
cb->start = 0;
cb->end = 0;
cb->buf = (char *)calloc(cb->size, sizeof(char));
}
void buf_Free(struct cBuf *cb) {
free(cb->buf);
}
int buf_IsFull(struct cBuf *cb) {
return (cb->end + 1) % cb->size == cb->start;
}
int buf_IsEmpty(struct cBuf *cb) {
return cb->end == cb->start;
}
int buf_Insert(struct cBuf *cb, char *elem) {
int i,j;
pthread_mutex_lock(&(cb->mutex));
for (i=0; i < strlen(elem); ++ i){
if (buf_IsFull(cb)==1) printf("\nProducer (buf_Insert) is waiting ");
while(buf_IsFull(cb)){
pthread_cond_wait(&(cb->buffer_empty),&(cb->mutex));
}
cb->buf[cb->end] = elem[i];
cb->end = (cb->end + 1) % cb->size;
printf("%c-",elem[i]);
}
pthread_cond_signal(&(cb->buffer_full));
pthread_mutex_unlock(&(cb->mutex));
return 0;
}
int buf_Read(struct cBuf *cb, char *out) {
int i,j;
pthread_mutex_lock(&(cb->mutex));
if (buf_IsEmpty(cb))printf("\nConsumer (buf_Read) is waiting ");
while(buf_IsEmpty(cb)){
pthread_cond_wait(&(cb->buffer_full),&(cb->mutex));
}
for (i=0;i<BUFFER_SIZE-1;i++){
if (cb->start == cb->end) break;
out[i] = cb->buf[cb->start];
cb->buf[cb->start] = '_';
cb->start = (cb->start + 1) % cb->size;
printf("%c-",out[i]);
}
pthread_cond_signal(&(cb->buffer_empty));
pthread_mutex_unlock(&(cb->mutex));
return 0;
}
void * client(void *cb){
pthread_detach(pthread_self());
struct cBuf *myData;
myData = (struct cBuf*) cb;
char input[]="Hello World!";
if (buf_Insert(myData, input)) printf("\n");
return 0;
}
int main(void) {
char out[60];
pthread_t thread;
int i;
pthread_cond_init(&(cb.buffer_full),NULL);
pthread_cond_init(&(cb.buffer_empty),NULL);
buf_Init(&cb, BUFFER_SIZE);
for (i = 0; i<1; i++){
if(pthread_create (&thread,NULL, client, (void *) &cb) !=0){
#ifdef DEBUG
printf("\nDEBUG (Main Thread) - Error while creating thread");
#endif
} else {
#ifdef DEBUG
printf("\nDEBUG (Main Thread) - Thread created");
#endif
}
}
while (1){
if (buf_Read(&cb,out)) printf ("succes");
}
buf_Free(&cb);
return 0;
}
它主要在缓冲区大于单个客户端的消息时起作用(通过buffer_size
增大,例如 16)。然而,当我把它变小时,它似乎陷入僵局,即使经过大量研究,我也无法弄清楚为什么。当我在调试器中运行代码时,代码似乎停止运行
pthread_cond_wait(&(cb->buffer_empty),&(cb->mutex));
为什么代码在这里停顿,如何防止它停顿?