试试用子线程,如:
#include <stdio.h>
#include <pthread.h>
void *_print(void *par)
{
char k = (char)(*((char*)par));
char ch;
while ((ch=getch()) != '0')
printf("%c %c\n", k, ch);
return NULL;
}
int main(void)
{
pthread_t t[2];
char p[2]= {'a','b'};
if(pthread_create(&t[0], NULL, _print, &p[0]) == -1)
{
puts("fail to create pthread t0");
exit(1);
}
if(pthread_create(&t[1], NULL, _print, &p[1]) == -1)
{
puts("fail to create pthread t1");
exit(1);
}
void * ret;
if(pthread_join(t[0], &ret) == -1)
{
puts("fail to recollect t0");
exit(1);
}
if(pthread_join(t[1], &ret) == -1)
{
puts("fail to recollect t1");
exit(1);
}
return 0;
}
[此贴子已经被作者于2018-10-13 06:54编辑过]