SHARE
TWEET

pawła

a guest May 21st, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <pthread.h>
  2. #include <iostream>
  3. #include <ncurses.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6.  
  7.  
  8. #define NUM_THREADS 10
  9. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  10. bool loop=true;
  11.  
  12. int los()
  13. {
  14.     return rand() % 7 - 3;
  15.  
  16. }
  17.  
  18.  
  19. void* Exit(void * arg)
  20. {
  21.  
  22.     int ch;
  23.  
  24.     ch=getch();
  25.     if (ch=='q')
  26.     {
  27.         loop=false;
  28.         pthread_exit(arg);
  29.     }
  30.  
  31.  
  32.  
  33.  
  34. }
  35.  
  36.  
  37.  
  38.  
  39. void* Rysuj(void * arg)
  40. {
  41.     srand (time(NULL));
  42.     int opoznienie=20000;
  43.     int x = 0, y = 0, max_x = 0, max_y = 0;
  44.     int next_x = 0;
  45.     int next_y = 0;
  46.     int direction_x = los();
  47.     int direction_y = 1;
  48.  
  49.     getmaxyx(stdscr, max_y, max_x);
  50.     x=max_x/2;
  51.     WINDOW *okno=newwin(1,1,y,x);
  52.  
  53.     while(opoznienie<500000)
  54.     {
  55.         usleep(opoznienie);
  56.  
  57.         pthread_mutex_lock(&mutex);
  58.         wclear(okno);
  59.         wrefresh(okno);
  60.         mvwin(okno, y,x);
  61.  
  62.         wprintw(okno,"o");
  63.         wrefresh(okno);
  64.  
  65.  
  66.  
  67.         pthread_mutex_unlock(&mutex);
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.         next_x = x + direction_x;
  75.         next_y = y + direction_y;
  76.  
  77.         if (next_x >= max_x || next_x < 0)
  78.         {
  79.             opoznienie*=2;
  80.             direction_x*= -1;
  81.         }
  82.         else
  83.         {
  84.             x+= direction_x;
  85.         }
  86.  
  87.         if (next_y >= max_y || next_y < 0)
  88.         {
  89.             opoznienie*=2;
  90.             direction_y*= -1;
  91.         }
  92.         else
  93.         {
  94.             y+= direction_y;
  95.         }
  96.  
  97.     }
  98.     wclear(okno);
  99.     wrefresh(okno);
  100.     pthread_exit(arg);
  101. }
  102.  
  103. void* Produkcja(void * arg)
  104. {
  105.     pthread_t tmp;
  106.     while (loop==true)
  107.     {
  108.         usleep(1000000);
  109.         pthread_create(&tmp, NULL, Rysuj, NULL);
  110.  
  111.  
  112.     }
  113.     pthread_join(tmp, NULL);
  114.     pthread_exit(arg);
  115.  
  116. }
  117.  
  118. int main()
  119. {
  120.     initscr();
  121.     noecho();
  122.     curs_set(FALSE);
  123.     pthread_t watek_wyjscia;
  124.     pthread_t kreator;
  125.     //pthread_t watki[NUM_THREADS];
  126.  
  127.     pthread_create(&watek_wyjscia, NULL, Exit, NULL);
  128.  
  129.     //pthread_create(&kreator, NULL, Produkcja, NULL);
  130.     //pthread_join(kreator, NULL);
  131.  
  132.  
  133.     while (loop==true)
  134.     {
  135.         pthread_t tmp;
  136.         usleep(1000000);
  137.         pthread_create(&tmp, NULL, Rysuj, NULL);
  138.         pthread_join(tmp, NULL);
  139.  
  140.     }
  141.  
  142.  
  143.  
  144.     endwin();
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top