日期:2014-05-16 浏览次数:20737 次
实在不好意思,第一个版本有些问题,是局部变量和堆里面变量的区别。今天做了一下修改。代码如下。
#ifndef _LIST_H_ #define _LIST_H_ #include <string.h> #include <stdio.h> #include <stdlib.h> struct List { char * buffer; char * cursor; char * begin; char * end; }; // you must add struct, otherwise it will prompt "expected ‘)’ before ‘*’ token" void init(struct List * p_list); int get_buffer_length (struct List * p_list); void put_into_buffer(struct List* p_list,char *pc_char); char get_from_buffer(struct List* p_list); #endif
?
?? 主要是更改了结构体List的定义,buffer必须定义为堆里面的变量。
???#include "list.h"
void init(struct List * p_list){ char *temp=(char *)malloc(10); memset(temp,'\0',10); p_list->buffer=temp; p_list->cursor=(p_list->buffer)+10; p_list->begin=p_list->buffer; p_list->end=(p_list->buffer)+10; } int get_buffer_length(struct List * p_list){ return p_list->end-p_list->cursor; } void put_into_buffer(struct List * p_list,char * pc_char){ printf("put ........ %c\n",*pc_char); if(p_list->cursor<=p_list->begin||p_list->cursor>p_list->end) printf(" put error\n"); //put the char into cursor's former place (p_list->cursor)--; memset(p_list->cursor,(int)(*pc_char),1); } char get_from_buffer(struct List * p_list){ if(p_list->cursor<p_list->begin||p_list->cursor>p_list->end) printf(" get error\n"); int length=get_buffer_length(p_list); printf("list length now is %d\n",length); (p_list->cursor)++; char result=*(p_list->end-1); printf("get ------------------- %c\n",result); if(length==1) { return result; } else { char *temp=(char *)malloc(10); bcopy(p_list->cursor-1,temp+10-length+1,length-1); free(p_list->buffer); p_list->buffer=temp; p_list->begin=temp; p_list->end=temp+10; p_list->cursor=p_list->end-length+1; } return 0; }
?
?? ?#include <pthread.h>
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include "List.h" pthread_cond_t has_product = PTHREAD_COND_INITIALIZER; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; //define the Producer thread void * produce(void * arg) { //arg is a char * pointer struct List * p_list=(struct List *)arg; //if buffer length =10,than wait,because buffer is full, while(1) { pthread_mutex_lock(&lock); //正确位置, //printf("111111\n"); if(get_buffer_length(p_list)==10) { //printf("22222222\n"); int num1=pthread_cond_wait(&has_product, &lock); //printf("33333333\n"); //printf(" num11111 %d\n",num1); } //buffer'length !=10,not full, we can put char into buffer. //printf("444444\n"); //pthread_mutex_lock(&lock); //错误位置 //printf("555555\n"); char zifu=(char)(1+(int)(128.0*rand()/(RAND_MAX+1.0))); put_into_buffer(p_list,&zifu); //after put, if the length>0,we can notify the wait thread,means that you can get char now if(get_buffer_length(p_list)==1) { pthread_cond_signal(&has_product); //printf("66666\n"); } //printf("777777777777\n"); int num3=pthread_mutex_unlock(&lock); //printf("unlocknumber 1111111111111 is %d\n",num3); //printf("888888888888\n"); sleep(1); //printf("99999999\n"); } } //define the second thread void * comsume(void * arg) { //arg is a char * pointer struct List * p_list=(struct List *)arg; while(1) { //if buffer length =10,than wait,because buffer is full, pthread_mutex_lock(&lock); //正确位置, //printf("aaaaaaaaaa\n"); if(get_buffer_length(p_list)==0) { //printf("bbbbbbbbbbbbbb\n"); int num2=pthread_cond_wait(&has_product, &lock); //printf("cccccccccc