Guest User

Untitled

a guest
Jul 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #include <unistd.h>
  8. #include <pthread.h>
  9. #include <semaphore.h>
  10.  
  11. #define THREAD_COUNT 4
  12.  
  13. pthread_t thread[THREAD_COUNT];
  14.  
  15. typedef struct task_struct
  16. {
  17. void (*f)(void *p);
  18. void *argv;
  19. struct task_struct *next;
  20. } task_t;
  21.  
  22. static sem_t task_count;
  23. static pthread_mutex_t task_mutex;
  24. static task_t *task_root;
  25.  
  26. task_t* task_new(void (*f)(void *p), void *argv)
  27. {
  28. task_t *task;
  29.  
  30. task = malloc(sizeof(task_t));
  31. task->f = f;
  32. task->argv = argv;
  33. task->next = NULL;
  34.  
  35. return task;
  36. }
  37.  
  38. void task_run(task_t *task)
  39. {
  40. task->f(task->argv);
  41. }
  42.  
  43. void task_destroy(task_t *task)
  44. {
  45. free(task);
  46. }
  47.  
  48. void* thread_body(void *p)
  49. {
  50. task_t *task;
  51.  
  52. printf("Vlakno: %d\n", p);
  53.  
  54. while(1)
  55. {
  56.  
  57. sem_wait(&task_count);
  58.  
  59. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  60.  
  61. pthread_mutex_lock(&task_mutex);
  62.  
  63. task = task_root;
  64. task_root = task_root->next;
  65.  
  66. pthread_mutex_unlock(&task_mutex);
  67.  
  68. //printf("Vlakno: X%d\n", p);
  69. task_run(task);
  70. task_destroy(task);
  71.  
  72. fflush(stdout);
  73. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  74. }
  75.  
  76. return NULL;
  77. }
  78.  
  79. int work_init()
  80. {
  81. int i;
  82.  
  83. sem_init(&task_count, 0, 0);
  84. pthread_mutex_init(&task_mutex, NULL);
  85.  
  86. task_root = NULL;
  87.  
  88. for(i = 0; i<THREAD_COUNT; i++)
  89. {
  90. pthread_create(&thread[i], NULL, thread_body, i);
  91. pthread_detach(thread[i]);
  92. }
  93.  
  94. return 0;
  95. }
  96.  
  97. int work_add_task(task_t *task)
  98. {
  99. task_t *task_actual;
  100. task_t *task_prev;
  101.  
  102. pthread_mutex_lock(&task_mutex);
  103.  
  104. if( task_root == NULL )
  105. {
  106. task_root = task;
  107.  
  108. pthread_mutex_unlock(&task_mutex);
  109. sem_post(&task_count);
  110.  
  111. return;
  112. }
  113.  
  114. task_prev = NULL;
  115. task_actual = task_root;
  116.  
  117. while( task_actual != NULL )
  118. {
  119. task_prev = task_actual;
  120. task_actual = task_actual->next;
  121. }
  122.  
  123. task_prev->next = task;
  124.  
  125. pthread_mutex_unlock(&task_mutex);
  126.  
  127. sem_post(&task_count);
  128.  
  129. return 0;
  130. }
  131.  
  132. int work_cancel()
  133. {
  134. int i;
  135.  
  136. for(i = 0; i<THREAD_COUNT; i++)
  137. {
  138. printf("cancel %p %d\n", &thread[i], i);
  139. pthread_cancel(thread[i]);
  140. }
  141.  
  142. return 0;
  143. }
  144.  
  145. int work_quit()
  146. {
  147. int i;
  148.  
  149. pthread_mutex_destroy(&task_mutex);
  150.  
  151. for(i = 0; i<THREAD_COUNT; i++)
  152. {
  153. pthread_join(thread[i], NULL);
  154. }
  155.  
  156. return 0;
  157. }
  158.  
  159. void action(void *p)
  160. {
  161. printf("%s\n", (char *)p);
  162. free(p);
  163. }
  164.  
  165. int main()
  166. {
  167. int i;
  168.  
  169. work_init();
  170.  
  171. for(i = 0; i<100; i++)
  172. {
  173. char str[64];
  174.  
  175. sprintf(str, "uloha %d", i);
  176. work_add_task(task_new(action, (void *)strdup(str)));
  177. }
  178.  
  179. sleep(2);
  180.  
  181. //work_cancel();
  182.  
  183. work_quit();
  184.  
  185. return 0;
  186. }
Add Comment
Please, Sign In to add comment