Advertisement
rnort

SPO-4-NIX-RC1

Nov 9th, 2012
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 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_PROCESS_NUMBER 10
  10.  
  11. void * func( void* );
  12. pthread_mutex_t mu;
  13. int quitThread[MAX_PROCESS_NUMBER];
  14.  
  15. void initCurses()
  16. {
  17.     initscr();
  18.     clear();
  19.     noecho();
  20.     refresh();
  21.     curs_set(0);
  22. }
  23.  
  24. int main()
  25. {
  26.     pthread_mutex_init(&mu, NULL);
  27.     initCurses();
  28.     pthread_t handle[MAX_PROCESS_NUMBER];
  29.     int currentThreadCount = 0;
  30.     int isQuit = 0;
  31.     while(isQuit == 0)
  32.     {
  33.         fflush(stdin);
  34.         char c = getch();
  35.         switch ( c )
  36.         {
  37.             case '+':
  38.                 if ( currentThreadCount < MAX_PROCESS_NUMBER)
  39.                 {
  40.                     quitThread[currentThreadCount] = 0;
  41.                     if (pthread_create(&handle[currentThreadCount], NULL, func, (void*)currentThreadCount) != 0)
  42.                     {
  43.                         printw("pthread_create() failed with error %s\n", strerror(errno));
  44.                         refresh();
  45.                         break;
  46.                     }
  47.                     else
  48.                     {
  49.                         currentThreadCount++;
  50.                     }
  51.                    
  52.                 }
  53.                 break;
  54.             case '-':
  55.                 if(currentThreadCount != 0)
  56.                 {
  57.                     quitThread[--currentThreadCount] = 1;
  58.                     if (pthread_join(handle[currentThreadCount], NULL) != 0)
  59.                     {
  60.                         printw("pthread_join(%lu) failed with error %s\n", handle[currentThreadCount] ,strerror(errno));
  61.                         refresh();
  62.                     }
  63.                 }
  64.                 break;
  65.             case 'q':
  66.                 isQuit = 1;
  67.                 break;
  68.             default:
  69.                 break;
  70.         }
  71.     }
  72.     while( currentThreadCount > 0 )
  73.     {
  74.         quitThread[--currentThreadCount] = 1;
  75.         if (pthread_join(handle[currentThreadCount], 0) != 0)
  76.         {
  77.             printw("pthread_join(%lu) failed with error %s\n", handle[currentThreadCount], strerror(errno));
  78.             refresh();
  79.             break;
  80.         }
  81.     }
  82.     pthread_mutex_destroy(&mu);
  83.     echo();
  84.     endwin();
  85.     return 0;
  86. }
  87.  
  88. char textStrings[MAX_PROCESS_NUMBER][50] =
  89. {
  90.         "Nirvana ", // was an American rock band that was formed by singer/guitarist Kurt Cobain and bassist Krist Novoselic ",
  91.         "Queen ", //recorded the album A Kind of Magic, containing several reworkings of songs written for the fantasy action film Highlander. ",
  92.         "Scorpions ", //are a rock band from Hannover, Germany formed in 1965 by guitarist Rudolf Schenker. ",
  93.         "Metallica ", //has released nine studio albums, three live albums, five extended plays, 24 music videos, and 45 singles. ",
  94.         "Bon Jovi ", //is an American rock band from Sayreville, New Jersey. ",
  95.         "The Final Countdown ",// is the third studio album by the Swedish hard rock band Europe. Released on 26 May 1986. ",
  96.         "Rammstein ", //was founded  by guitarist Richard Z. Kruspe. In 1989, he escaped to West Berlin and started the band Orgasm Death Gimmicks. ",
  97.         "Louna ", //is a Russian Alternative/Punk rock band, formed in Moscow in 2008 by Tracktor Bowling's musicians Lusine Gevorkyan and Vitaly Demidenko ",
  98.         "Sonic Syndicate ",// is a heavy metal band from Falkenberg, Sweden. ",
  99.         "Skillet ",//is an American Christian rock band formed in Memphis, Tennessee in 1996. ",
  100.     };
  101.  
  102.  
  103. void* func(void* param)
  104. {
  105.     int a = (int)param;
  106.     while(quitThread[a] != 1)
  107.     {
  108.         if (pthread_mutex_trylock(&mu) == 0)
  109.         {
  110.             for (int i = 0; textStrings[a][i]; i++)
  111.             {  
  112.                 printw("%c", textStrings[a][i]);
  113.                 refresh();
  114.                 usleep(100000);
  115.             }
  116.             printw(" ");
  117.             refresh();
  118.             pthread_mutex_unlock(&mu);    
  119.         }
  120.     }
  121.     quitThread[a] = 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement