#include<stdio.h>
#include<pthread.h>
#include<termios.h>
static int peek_character = -1;
static struct termios initial_settings, new_settings;
void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}
int kbhit(){
char ch;
int nread;
if( peek_character!=-1){
return(1);
}
new_settings.c_cc[VMIN] = 0;
tcsetattr(0,TCSANOW,&new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
if(nread==1){
peek_character=ch;
return(1);
}
return 0;
}
void* thread(void *arg)
{
int a,b,res;
a=0;
b=1;
res=0;
while (1)
{
res=a+b;
a=b;
b=res;
sleep(1);
printf("%d\n",res);
pthread_testcancel();
}
}
int main(int argc,char *argv[])
{
pthread_t th;
int arg=0;
int *thread_ret=NULL;
pthread_create(&th,NULL,thread,&arg);
while(kbhit())
{
pthread_cancel(th);
break;
}
pthread_join(th,void(**)&thread_ret);
return 0;
}
4万+

被折叠的 条评论
为什么被折叠?



