Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <pthread.h>
  8.  
  9. #define STRING_LIMIT 100
  10.  
  11. static pthread_mutex_t mute_lock;
  12. static pthread_cond_t conditional;
  13.  
  14. struct str_array_t{
  15. bool quit;
  16. size_t num_strings;
  17. char** data;
  18. };
  19.  
  20. void add_to_array(str_array_t* array, const char* str)
  21. {
  22. if(array->num_strings >= STRING_LIMIT)
  23. {
  24. free(array->data[array->num_strings % STRING_LIMIT]);
  25. array->data[array->num_strings++ % STRING_LIMIT] = strdup(str);
  26. }
  27. else
  28. {
  29. array->data[array->num_strings++] = strdup(str);
  30. }
  31. return;
  32. }
  33.  
  34. const char* get_random_word()
  35. {
  36. static const char* words[9] = {"hello", "world", "fancy", "cruel", "school", "tool", "string", "bling"};
  37. return words[rand()%8];
  38. }
  39.  
  40. void* producer(void* a)
  41. {
  42. str_array_t *array = (str_array_t*)a;
  43. char string[100];
  44. while(array->quit != true)
  45. {
  46. sleep(rand()%3);
  47. pthread_mutex_lock(&mute_lock);
  48. add_to_array(array, get_random_word());
  49. pthread_mutex_unlock(&mute_lock);
  50. pthread_cond_signal(&conditional);
  51. }
  52. free(array);
  53. exit(EXIT_SUCCESS);
  54. //return;
  55. }
  56.  
  57. void* consumer(void* a)
  58. {
  59. str_array_t *array = (str_array_t*)a;
  60. pthread_mutex_lock(&mute_lock);
  61. size_t index = 0;
  62. size_t size = array->num_strings;
  63. while(size == 0)
  64. {
  65. pthread_cond_wait(&conditional, &mute_lock);
  66. size = array->num_strings;
  67. if(array->quit){ exit(EXIT_SUCCESS); }
  68. }
  69.  
  70. while(array->quit != true){
  71. while(index < array->num_strings)
  72. {
  73. printf("got %s\n", array->data[index < STRING_LIMIT ? index++ : index++ % STRING_LIMIT]);
  74. sleep(rand() % 3);
  75. }
  76. pthread_mutex_unlock(&mute_lock);
  77. sleep(10);
  78. pthread_mutex_lock(&mute_lock);
  79. }
  80. free(array);
  81. exit(EXIT_SUCCESS);
  82.  
  83. }
  84.  
  85. int main(int argc, char* argv[])
  86. {
  87. pthread_t producer_thread, consumer_thread;
  88. str_array_t* array = new str_array_t;
  89. array->quit = false;
  90. array->num_strings = 0;
  91. array->data = new char*;
  92. int producer_flag, consumer_flag;
  93. producer_flag = pthread_create( &producer_thread, NULL, producer, (void*)array);
  94. if(producer_flag)
  95. {
  96. printf("errno %d\n", producer_flag);
  97. return EXIT_FAILURE;
  98. }
  99. consumer_flag = pthread_create( &consumer_thread, NULL, consumer, (void*)array);
  100. if(producer_flag)
  101. {
  102. printf("errno %d\n", consumer_flag);
  103. return EXIT_FAILURE;
  104. }
  105.  
  106. char asd_msg[250] = "";
  107. bool asd_queued = false;
  108. const char* c_string;
  109. while(1){
  110. scanf("%s", asd_msg);
  111. if(strcmp(asd_msg, "quit") == 0){
  112. array->quit = true;
  113. exit(EXIT_SUCCESS);
  114. }
  115. asd_queued = true;
  116. while(asd_queued){
  117. pthread_mutex_lock(&mute_lock);
  118. add_to_array(array, asd_msg);
  119. pthread_mutex_unlock(&mute_lock);
  120. asd_queued = false;
  121. }
  122. sleep(1);
  123. }
  124.  
  125. return EXIT_SUCCESS;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement