#include "string.h"

#include "stdio.h"
#include "pthread.h"
#include "stdlib.h"
 
struct data{
 int num;
 char str[20];
 struct data *next;
};
struct data *all;
 
pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t flag=PTHREAD_COND_INITIALIZER;
void do_work()
{
while(1)
{
 pthread_mutex_lock(&lock);
  if(all == NULL)
   {
   printf("thread is waiting...\n");
    pthread_cond_wait(&flag,&lock);
  printf("thread got data...\n");
   }
  struct data *tmp;
  tmp=all;
  all=all->next;
 pthread_mutex_unlock(&lock);
 printf("%d,%s\n",tmp->num,tmp->str);
 free(tmp);
}
}
 
void main(int argc,char *argv[])
{
    pthread_t go;
    pthread_create(&go,NULL,do_work,NULL);
    pthread_detach(go);
   while(1)
   {
    int num;
    char str[20];
    printf("plz input num and str\n");
    scanf("%d,%s",&num,str);
    pthread_mutex_lock(&lock);
    printf("main write data...\n");
    struct data *main1;
    main1=(struct data *)malloc(sizeof(struct data));
    main1->num=num;
    strcpy(main1->str,str);
    main1->next=NULL;
   if(all == NULL)
    all=main1;
   else{
        strcut data *tmp1;
        tmp1=all;
        while(tmp1->next !=NULL)
           tmp1=tmp1->next;
        tmp1->next=main1;
    }
    printf("main write over data...\n");
    pthread_cond_signal(&flag);
    pthread_mutex_unlock(&lock);
   }
}