Linux 线程 动画
来源:互联网 发布:mac快捷键缩小窗口 编辑:程序博客网 时间:2024/06/05 15:26
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <curses.h>#include <pthread.h>#include <unistd.h>#define MAXMSG 24#define TUNIT 20000struct propset{char *str;int row;int delay;int dir;};pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;int setup(int nstrings,char*string[],struct propset props[]);void *animate(void*);int main(int ac,char *av[]){int c;pthread_t threads[MAXMSG];struct propset props[MAXMSG];int num_msg;int i;if(ac == 1){printf("usage:tanimate string...\n");exit(1);}num_msg = setup(ac-1,av+1,props);/* create all the threads */for(i=0;i<num_msg;i++){if(pthread_create(&threads[i],NULL,animate,&props[i])){fprintf(stderr,"error creating thread");endwin();exit(0);}}while(1){char c = getch();if(c == 'Q') break;if(c == ' ')for(i=0;i<num_msg;i++)props[i].dir = -props[i].dir;if(c>='0' && c<='9'){i=c-'0';if(i<num_msg)props[i].dir = -props[i].dir;}}/* cancel all the threads */pthread_mutex_lock(&mx);for(i=0;i<num_msg;i++)pthread_cancel(threads[i]);endwin();return 0;}int setup(int nstrings,char *strings[],struct propset props[]){int num_msg = (nstrings > MAXMSG ? MAXMSG : nstrings);int i;/* assign rows and velocities to each string */srand(getpid());for(i=0;i<num_msg;i++){props[i].str = strings[i];props[i].row = rand()%24-1;props[i].delay = 1+rand()%5;props[i].dir = (rand()%2?1:-1);}/* set up curses */initscr();crmode();noecho();clear();mvprintw(LINES-1,0,"'Q' to quit,'0'..'%d' to bounce",num_msg-1);return num_msg;}/* the code that runs in each thread */void *animate(void *arg){struct propset *info = arg;int len = strlen(info->str)+2;int col = rand()%(COLS - len - 3);while(1){usleep(info->delay*TUNIT);pthread_mutex_lock(&mx);move(info->row,col);addch(' ');addstr(info->str);addch(' ');move(LINES-1,COLS-1);refresh();pthread_mutex_unlock(&mx);/* move item to next column and check for bouncing */col+=info->dir;if(col<=0 && info->dir == -1)info->dir=1;else if(col + len >= COLS && info->dir == 1)info->dir=-1;}}
gcc tanimate.c -lcurses -lpthread -o tanimate
0 0
- Linux 线程 动画
- 线程控制动画
- 动画和线程的关系?
- linux 线程 线程属性
- linux 线程 线程同步
- linux 线程
- linux线程
- Linux 线程
- linux 线程
- linux线程
- Linux “线程”
- Linux线程
- linux 线程
- linux 线程
- linux线程
- linux 线程
- linux--线程
- Linux线程
- 菜鸟学算法之POJ1636 Prison Rearrangement
- 第十九篇:学习好C++需要的读的书籍
- 2D Conforming Triangulations
- Ajax异步加载问题
- Oracle pl/sql编程 2---plsql变量与常量
- Linux 线程 动画
- 快速删除docker中的容器
- 非递归快速排序
- 巴什博奕
- 毕业生自传-9(落地生根北漂人)
- JS基础
- require头域
- python保存文件UnicodeEncodeError以及reload(sys)后print失效问题
- 数据挖掘导论总结之分类技术