并发无锁环形队列的实现

1: #include "UnlockQueue.h" 2: #include <iostream> 3: #include <algorithm> 4: #include <pthread.h> 5: #include <time.h> 6: #include <stdio.h> 7: #include <errno.h> 8: #include <string.h> 9:  10: struct student_info 11: { 12:    long stu_id; 13:    unsigned int age; 14:    unsigned int score; 15: }; 16:  17: void print_student_info(const student_info *stu_info) 18: { 19:    if(NULL == stu_info) 20:        return; 21:  22:    printf("id:%ld\t",stu_info->stu_id); 23:    printf("age:%u\t",stu_info->age); 24:    printf("score:%u\n",stu_info->score); 25: } 26:  27: student_info * get_student_info(time_t timer) 28: { 29:      student_info *stu_info = (student_info *)malloc(sizeof(student_info)); 30:      if (!stu_info) 31:      { 32:        fprintf(stderr, "Failed to malloc memory.\n"); 33:        return NULL; 34:      } 35:      srand(timer); 36:      stu_info->stu_id = 10000 + rand() % 9999; 37:      stu_info->age = rand() % 30; 38:      stu_info->score = rand() % 101; 39:      //print_student_info(stu_info); 40:      return stu_info; 41: } 42:  43: void * consumer_proc(void *arg) 44: { 45:      UnlockQueue* queue = (UnlockQueue *)arg; 46:      student_info stu_info; 47:      while(1) 48:      { 49:          sleep(1); 50:          unsigned int len = queue->Get((unsigned char *)&stu_info, sizeof(student_info)); 51:          if(len > 0) 52:          { 53:              printf("------------------------------------------\n"); 54:              printf("UnlockQueue length: %u\n", queue->GetDataLen()); 55:              printf("Get a student\n"); 56:              print_student_info(&stu_info); 57:              printf("------------------------------------------\n"); 58:          } 59:      } 60:      return (void *)queue; 61: } 62:  63: void * producer_proc(void *arg) 64:  { 65:      time_t cur_time; 66:      UnlockQueue *queue = (UnlockQueue*)arg; 67:      while(1) 68:      { 69:          time(&cur_time); 70:          srand(cur_time); 71:          int seed = rand() % 11111; 72:          printf("******************************************\n"); 73:          student_info *stu_info = get_student_info(cur_time + seed); 74:          printf("put a student info to queue.\n"); 75:          queue->Put( (unsigned char *)stu_info, sizeof(student_info)); 76:          free(stu_info); 77:          printf("UnlockQueue length: %u\n", queue->GetDataLen()); 78:          printf("******************************************\n"); 79:          sleep(1); 80:      } 81:      return (void *)queue; 82:  } 83:  84:  85: int main() 86: { 87:    UnlockQueue unlockQueue(1024); 88:    if(!unlockQueue.Initialize()) 89:    { 90:        return -1; 91:    } 92:  93:    pthread_t consumer_tid, producer_tid; 94:  95:    printf("multi thread test.......\n"); 96:  97:    if(0 != pthread_create(&producer_tid, NULL, producer_proc, (void*)&unlockQueue)) 98:    { 99:          fprintf(stderr, "Failed to create consumer thread.errno:%u, reason:%s\n", 100:                  errno, strerror(errno)); 101:          return -1; 102:    } 103:  104:    if(0 != pthread_create(&consumer_tid, NULL, consumer_proc, (void*)&unlockQueue)) 105:    { 106:            fprintf(stderr, "Failed to create consumer thread.errno:%u, reason:%s\n", 107:                    errno, strerror(errno)); 108:            return -1; 109:    } 110:  111:    pthread_join(producer_tid, NULL); 112:    pthread_join(consumer_tid, NULL); 113:  114:    return 0; 115:  }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e7a47a26563e8ac0b8b37173c9f82367.html