[英]c - scanf interrupted, and printf in thread
我有一个打印数字的简单线程,问题是该线程打印在scanf上。 这样的事情。
input> DATAOFTHREAD
但我想打印结果像这样
DATAOFTHREAD
input>
有可能的? 我应该使用什么功能? 这是我的代码:
#include <stdio.h>
#include <pthread.h>
void *connection_handler(void* data) {
int i = (int)data;
for(i=0;i<5;i++) {
printf("%d", i);
fflush(stdout);
}
pthread_exit(NULL);
}
int main()
{
int t;
int x;
int rc;
pthread_t thread_id;
rc = pthread_create(&thread_id, NULL, connection_handler, (void *)x);
if(rc) {
printf("Error en pthread()\n");
return 1;
}
printf("Ingresa un numero: ");
scanf("%d", &t);
printf("%d\n", t);
pthread_exit(NULL);
return 0;
}
谢谢
您可以调用pthread_join,以便仅在线程完成后才执行scanf:
pthread_join(&thread_id, NULL);
printf("Ingresa un numero: ");
scanf("%d", &t);
顺便说一句,您不需要在main()中调用pthread_exit。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.