Advertisement
GTnik

Untitled

Apr 23rd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #define MAX 10000
  7.  
  8. struct TContext {
  9.     const char* text;
  10. };
  11.  
  12. pthread_mutex_t mutex;
  13. pthread_cond_t cond;
  14.  
  15. void* ThreadFunc(void* arg) {
  16.     struct TContext* ctxt = arg;
  17.  
  18.     for (int i = 0; i < MAX; ++i) {
  19.         printf("%s", ctxt->text);
  20.     }
  21.  
  22.     pthread_exit(0);
  23. }
  24.  
  25. int main() {
  26.     pthread_mutex_init(&mutex, NULL);
  27.     pthread_cond_init(&cond, NULL);
  28.  
  29.     pthread_t t1;
  30.     pthread_t t2;
  31.     pthread_t t3;
  32.  
  33.     int counter = 0;
  34.     struct TContext ctxt1 = {"a"};
  35.     struct TContext ctxt2 = {"b"};
  36.     struct TContext ctxt3 = {"c"};
  37.     pthread_create(&t1, 0, ThreadFunc, &ctxt1);
  38.     pthread_create(&t2, 0, ThreadFunc, &ctxt2);
  39.     pthread_create(&t3, 0, ThreadFunc, &ctxt3);
  40.  
  41.     pthread_join(t1, 0);
  42.     pthread_join(t2, 0);
  43.     pthread_join(t3, 0);
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement