Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #define _POSIX_C_SOURCE 200809L
  2. #include <ncurses.h>
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #define CUBE_LENGTH 12
  10. #define NUM_THREADS 2
  11. #define test_errno(msg) do{if (errno) {perror(msg); exit(EXIT_FAILURE);}} while(0);
  12.  
  13. pthread_mutex_t blokada;
  14.  
  15.  
  16. void* perform_work(void* argument) {
  17.   int passed_in_value;
  18.   int counter = 0;
  19.   passed_in_value = *((int*) argument);
  20.  // printf("Hello World! It's me, thread with argument %d!\n", passed_in_value);
  21.   int y=2*CUBE_LENGTH,x=2*CUBE_LENGTH;
  22.   int y_,x_;
  23. //  const char * z="O",* r=" ";
  24.   char z='O',r=' ';
  25.  
  26.   if     (passed_in_value==0){ x_=-1; y_=-1;  }
  27.   else if(passed_in_value==1){ x_=-2; y_=-1;  }
  28.   else if(passed_in_value==2){ x_=-1; y_=-2;  }
  29.   else   {mvprintw(CUBE_LENGTH+2, 0, "Error, program gives wrong direction\n" ); }
  30.  
  31.   mvaddch(y, x, z);
  32.   refresh();
  33.   bool first=true;
  34.   while(counter<5)
  35.   {
  36. //  #ifdef BLOKADA
  37.         errno  = pthread_mutex_lock(&blokada);
  38.         test_errno("pthread_mutex_lock");
  39. //  #endif
  40.                 mvaddch(y, x, z);
  41.                 refresh();
  42.                 sleep(1);
  43.                 mvaddch(y, x, r);
  44.                 refresh();
  45.                 sleep(1);
  46. //  #ifdef BLOKADA
  47.         errno = pthread_mutex_unlock(&blokada);
  48.         test_errno("pthread_mutex_unlock");
  49. //  #endif
  50.  if(!first){
  51.                 //if((x==CUBE_LENGTH && y==CUBE_LENGTH+1) || (x==CUBE_LENGTH+1 && y==2*CUBE_LENGTH) || (x==2*CUBE_LENGTH+1 && y==CUBE_LENGTH) || (x==2*CUBE_LENGTH+1 && y==2*CUBE_LENGTH))
  52.                 //{x_*=-1; y_*=-1;counter++;}   //na to nie patrzcie ^
  53.                 if(x==CUBE_LENGTH || x==2*CUBE_LENGTH){ x_*=-1; counter++;}
  54.                 if(y==CUBE_LENGTH || y==2*CUBE_LENGTH){ y_*=-1; counter++;}
  55.         }else first=false;
  56.                 x+=x_; y+=y_;
  57.  
  58.   }
  59.  
  60.   return NULL;
  61. }
  62.  
  63.  
  64.  
  65. int main(int argc, char** argv) {
  66.   pthread_t threads[NUM_THREADS];
  67.   int thread_args[NUM_THREADS];
  68.   int result_code;
  69.   unsigned index;
  70.   const char * ramka="+";
  71.  
  72.   initscr();
  73.   int i,j;
  74.   for(i=CUBE_LENGTH-1;i<=2*CUBE_LENGTH+1;i++) for(j=CUBE_LENGTH-1;j<=2*CUBE_LENGTH+1;j++)
  75.   {
  76.         if(i==CUBE_LENGTH-1 || j== CUBE_LENGTH-1 || i==2*CUBE_LENGTH+1 || j==2*CUBE_LENGTH+1) mvprintw(j, i, ramka);
  77.         refresh();
  78.   }
  79.  
  80.   errno = pthread_mutex_init(&blokada, NULL);
  81.   test_errno("pthread_mutex_init");
  82.  
  83.   for (index = 0; index < NUM_THREADS; ++index) {
  84.         thread_args[ index ] = index%3;
  85. //   result_code        = pthread_create(&threads[index], NULL, perform_work, &thread_args[index]);
  86.         errno = pthread_create(&threads[index], NULL, perform_work, &thread_args[index]);
  87. //    assert(!result_code);
  88.         test_errno("pthread_create");
  89.   }
  90.  
  91.  
  92.   for (index = 0; index < NUM_THREADS; ++index) {
  93. //    result_code = pthread_join(threads[index], NULL);
  94. //    assert(!result_code);
  95.         errno = pthread_join(threads[index], NULL);
  96.         test_errno("pthread_join");
  97.   }
  98.  
  99.   endwin();
  100.   printf("In main: All threads completed successfully\n");
  101.   exit(EXIT_SUCCESS);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement