目的はworkin()
、他の非常に重い計算が行われている間に、優先度の低いスレッドを作成することです。
wait()
たとえば、0.5 秒にする方法はありますか?
オンになっている間、端末への着信文字をブロックすることは可能wait()
ですか? 「処理中...」に印字された文字がめちゃくちゃなので、この部分は解決済みです。以下の解決策を参照してください。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *workin(){
int i=0,count=10;
char v[]={' ',' ','-','-','-',' ',' ',' ',' ',' '};
for(;;){
for(i=count-1;i>=0;i--){
v[(i+1)%(count)]=v[i];
}
fflush(stdout);
printf("\r");
printf("processing ");
printf("%s ",v);
sleep(1);
}
}
int main(int argc, char *argv[]){
pthread_t tid;
pthread_create(&tid,NULL,workin,NULL);
sleep(15);/*HEAVY stuff here*/
pthread_cancel(tid);
printf("\r\n");
return 0;
}
半分解決、行方不明wait(0.5)
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
char getch(){
/*#include <unistd.h> //_getch*/
/*#include <termios.h> //_getch*/
char buf=0;
struct termios old = {0};
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag&=~ICANON;
old.c_lflag&=~ECHO;
old.c_cc[VMIN]=1;
old.c_cc[VTIME]=0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0,&buf,1)<0)
perror("read()");
old.c_lflag|=ICANON;
old.c_lflag|=ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror ("tcsetattr ~ICANON");
return buf;
}
void *workin(){
int i=0,count=10;
char v[]={' ',' ','>','>','>',' ',' ',' ',' ',' '};
for(;;){
for(i=count-1;i>=0;i--){
v[(i+1)%(count)]=v[i];
}
fflush(stdout);
printf("processing ");
printf("%s ",v);
printf("\r");
sleep(1);
}
}
int main(int argc, char *argv[]){
char ch=0;
pthread_t tid;
pthread_create(&tid,NULL,workin,NULL);
do{
ch=getch();/*HEAVY CALCULATIONS HERE*/
}while(1);
pthread_cancel(tid);
printf("\r\n");
return 0;
}