Advertisement
rnort

SPO-4-NIX-FINAL

Nov 10th, 2012
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <ncurses.h>
  6. #include <errno.h>
  7. #include <cstring>
  8.  
  9. #define MAX_THREAD_NUMBER 10
  10.  
  11. void* writer( void* );
  12. void removeAll();
  13. int addThread();
  14. int removeThread();
  15. pthread_mutex_t mu;
  16. int quitThread[MAX_THREAD_NUMBER];
  17. int currentThreadCount = 0;
  18. pthread_t handle[MAX_THREAD_NUMBER];
  19.  
  20. void init()
  21. {
  22.     initscr();
  23.     clear();
  24.     noecho();
  25.     refresh();
  26.     curs_set(0);
  27.     pthread_mutex_init(&mu, NULL);
  28. }
  29.  
  30. int main()
  31. {
  32.     init();
  33.     int isQuit = 0;
  34.     while(isQuit == 0)
  35.     {
  36.         fflush(stdin);
  37.         char c = getch();
  38.         switch ( c )
  39.         {
  40.             case '+':
  41.                 addThread();    
  42.                 break;
  43.             case '-':
  44.                 removeThread();
  45.                 break;
  46.             case 'q':
  47.                 isQuit = 1;
  48.                 break;
  49.             default:
  50.                 break;
  51.         }
  52.     }
  53.     removeAll();
  54.     pthread_mutex_destroy(&mu);
  55.     echo();
  56.     endwin();
  57.     return 0;
  58. }
  59.  
  60. int removeThread()
  61. {
  62.     if(currentThreadCount != 0)
  63.     {
  64.         quitThread[--currentThreadCount] = 1;
  65.         if (pthread_join(handle[currentThreadCount], NULL) != 0)
  66.         {
  67.             printw("pthread_join(%lu) failed with error %s\n", handle[currentThreadCount] ,strerror(errno));
  68.             refresh();
  69.         }
  70.     }
  71.     return 0;
  72. }
  73.  
  74. int addThread()
  75. {
  76.     if ( currentThreadCount < MAX_THREAD_NUMBER)
  77.     {
  78.         quitThread[currentThreadCount] = 0;
  79.         if (pthread_create(&handle[currentThreadCount], NULL, writer, (void*)currentThreadCount) != 0)
  80.         {
  81.             printw("pthread_create() failed with error %s\n", strerror(errno));
  82.             refresh();
  83.             return 1;
  84.         }
  85.         else
  86.         {
  87.             currentThreadCount++;
  88.         }
  89.     }
  90.     return 0;
  91. }
  92.  
  93. void removeAll()
  94. {
  95.     while( currentThreadCount > 0 )
  96.     {
  97.         quitThread[--currentThreadCount] = 1;
  98.         if (pthread_join(handle[currentThreadCount], 0) != 0)
  99.         {
  100.             printw("pthread_join(%lu) failed with error %s\n", handle[currentThreadCount], strerror(errno));
  101.             refresh();
  102.         }
  103.     }
  104. }
  105.  
  106. char textStrings[MAX_THREAD_NUMBER][50] =
  107.     {
  108.         "0-Nirvana ",
  109.         "1-Queen ",
  110.         "2-Scorpions ",
  111.         "3-Metallica ",
  112.         "4-Bon Jovi ",
  113.         "5-The Final Countdown ",
  114.         "6-Rammstein ",
  115.         "7-Louna ",
  116.         "8-Sonic Syndicate ",
  117.         "9-Skillet "
  118.     };
  119.  
  120.  
  121. void* writer(void* param)
  122. {
  123.     int a = (int)param;
  124.     while(quitThread[a] != 1)
  125.     {
  126.         if (pthread_mutex_trylock(&mu) == 0)
  127.         {
  128.             for (int i = 0; textStrings[a][i]; i++)
  129.             {  
  130.                 printw("%c", textStrings[a][i]);
  131.                 refresh();
  132.                 usleep(100000);
  133.             }
  134.             printw(" ");
  135.             refresh();
  136.             pthread_mutex_unlock(&mu);    
  137.         }
  138.     }
  139.     quitThread[a] = 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement