Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <iostream>
- #include <ncurses.h>
- #include <unistd.h>
- #include <time.h>
- #define NUM_THREADS 10
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- bool loop=true;
- int los()
- {
- return rand() % 7 - 3;
- }
- void* Exit(void * arg)
- {
- int ch;
- ch=getch();
- if (ch=='q')
- {
- loop=false;
- pthread_exit(arg);
- }
- }
- void* Rysuj(void * arg)
- {
- srand (time(NULL));
- int opoznienie=20000;
- int x = 0, y = 0, max_x = 0, max_y = 0;
- int next_x = 0;
- int next_y = 0;
- int direction_x = los();
- int direction_y = 1;
- getmaxyx(stdscr, max_y, max_x);
- x=max_x/2;
- WINDOW *okno=newwin(1,1,y,x);
- while(opoznienie<500000)
- {
- usleep(opoznienie);
- pthread_mutex_lock(&mutex);
- wclear(okno);
- wrefresh(okno);
- mvwin(okno, y,x);
- wprintw(okno,"o");
- wrefresh(okno);
- pthread_mutex_unlock(&mutex);
- next_x = x + direction_x;
- next_y = y + direction_y;
- if (next_x >= max_x || next_x < 0)
- {
- opoznienie*=2;
- direction_x*= -1;
- }
- else
- {
- x+= direction_x;
- }
- if (next_y >= max_y || next_y < 0)
- {
- opoznienie*=2;
- direction_y*= -1;
- }
- else
- {
- y+= direction_y;
- }
- }
- wclear(okno);
- wrefresh(okno);
- pthread_exit(arg);
- }
- void* Produkcja(void * arg)
- {
- pthread_t tmp;
- while (loop==true)
- {
- usleep(1000000);
- pthread_create(&tmp, NULL, Rysuj, NULL);
- }
- pthread_join(tmp, NULL);
- pthread_exit(arg);
- }
- int main()
- {
- initscr();
- noecho();
- curs_set(FALSE);
- pthread_t watek_wyjscia;
- pthread_t kreator;
- //pthread_t watki[NUM_THREADS];
- pthread_create(&watek_wyjscia, NULL, Exit, NULL);
- //pthread_create(&kreator, NULL, Produkcja, NULL);
- //pthread_join(kreator, NULL);
- while (loop==true)
- {
- pthread_t tmp;
- usleep(1000000);
- pthread_create(&tmp, NULL, Rysuj, NULL);
- pthread_join(tmp, NULL);
- }
- endwin();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement