Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <ncurses.h>
- #include <errno.h>
- #include <cstring>
- #define MAX_PROCESS_NUMBER 10
- void * func( void* );
- pthread_mutex_t mu;
- int quitThread[MAX_PROCESS_NUMBER];
- void initCurses()
- {
- initscr();
- clear();
- noecho();
- refresh();
- curs_set(0);
- }
- int main()
- {
- pthread_mutex_init(&mu, NULL);
- initCurses();
- pthread_t handle[MAX_PROCESS_NUMBER];
- int currentThreadCount = 0;
- int isQuit = 0;
- while(isQuit == 0)
- {
- fflush(stdin);
- char c = getch();
- switch ( c )
- {
- case '+':
- if ( currentThreadCount < MAX_PROCESS_NUMBER)
- {
- quitThread[currentThreadCount] = 0;
- if (pthread_create(&handle[currentThreadCount], NULL, func, (void*)currentThreadCount) != 0)
- {
- printw("pthread_create() failed with error %s\n", strerror(errno));
- refresh();
- break;
- }
- else
- {
- currentThreadCount++;
- }
- }
- break;
- case '-':
- if(currentThreadCount != 0)
- {
- quitThread[--currentThreadCount] = 1;
- if (pthread_join(handle[currentThreadCount], NULL) != 0)
- {
- printw("pthread_join(%lu) failed with error %s\n", handle[currentThreadCount] ,strerror(errno));
- refresh();
- }
- }
- break;
- case 'q':
- isQuit = 1;
- break;
- default:
- break;
- }
- }
- while( currentThreadCount > 0 )
- {
- quitThread[--currentThreadCount] = 1;
- if (pthread_join(handle[currentThreadCount], 0) != 0)
- {
- printw("pthread_join(%lu) failed with error %s\n", handle[currentThreadCount], strerror(errno));
- refresh();
- break;
- }
- }
- pthread_mutex_destroy(&mu);
- echo();
- endwin();
- return 0;
- }
- char textStrings[MAX_PROCESS_NUMBER][50] =
- {
- "Nirvana ", // was an American rock band that was formed by singer/guitarist Kurt Cobain and bassist Krist Novoselic ",
- "Queen ", //recorded the album A Kind of Magic, containing several reworkings of songs written for the fantasy action film Highlander. ",
- "Scorpions ", //are a rock band from Hannover, Germany formed in 1965 by guitarist Rudolf Schenker. ",
- "Metallica ", //has released nine studio albums, three live albums, five extended plays, 24 music videos, and 45 singles. ",
- "Bon Jovi ", //is an American rock band from Sayreville, New Jersey. ",
- "The Final Countdown ",// is the third studio album by the Swedish hard rock band Europe. Released on 26 May 1986. ",
- "Rammstein ", //was founded by guitarist Richard Z. Kruspe. In 1989, he escaped to West Berlin and started the band Orgasm Death Gimmicks. ",
- "Louna ", //is a Russian Alternative/Punk rock band, formed in Moscow in 2008 by Tracktor Bowling's musicians Lusine Gevorkyan and Vitaly Demidenko ",
- "Sonic Syndicate ",// is a heavy metal band from Falkenberg, Sweden. ",
- "Skillet ",//is an American Christian rock band formed in Memphis, Tennessee in 1996. ",
- };
- void* func(void* param)
- {
- int a = (int)param;
- while(quitThread[a] != 1)
- {
- if (pthread_mutex_trylock(&mu) == 0)
- {
- for (int i = 0; textStrings[a][i]; i++)
- {
- printw("%c", textStrings[a][i]);
- refresh();
- usleep(100000);
- }
- printw(" ");
- refresh();
- pthread_mutex_unlock(&mu);
- }
- }
- quitThread[a] = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement