非阻塞输入C.

问题描述 投票:1回答:2

我正在尝试创建一个程序,用户被问到一个问题,并有几秒钟回答问题,否则程序停止输入。

现在我的问题是我无法让我的程序不阻止输入。我能够输入数据,但是当我没有并且计时器用完时,它一直要求输入。

我在Windows上运行并使用Code :: Blocks,以防它重要。如果有人能向我解释我做错了什么,我们将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <conio.h>

int key = 0;
int GradeTotal = 0;

//runs an empty loop every iteration F.E. for loop
void timer(int seconds)
{
    clock_t wait = (clock() + (seconds * CLOCKS_PER_SEC));
    while(clock() < wait){}

}

void timeleft()
{
    int index;

    for(index = 5; index >= 0; index--)
    {
        if(key != 0)
        {
            pthread_exit(timeleft);
        }

        timer(1);

        if(index == 0)
        {
        printf("\n\nTime's up!");
        }
    }
}

void questions()
{
    int key;

    printf("what is 1 + 1?\nAnswer: ");

    while(1)
    {
        if(_kbhit())
        {
            key = _getch();
            printf("%c",key);
            break;
        }
    }
    if(key == 50)
    {
         GradeTotal += 1;
    }  
 }


int main()
{
    pthread_t thread1,thread2;

    int index;
    int seconds = 0;
    pthread_create(&thread1, NULL, questions, NULL);
    pthread_create(&thread2, NULL, timeleft, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    printf("\n\nGrade: %d",GradeTotal);

    return 0;
}
c windows io pthreads nonblocking
2个回答
1
投票

当时间用完时,timeleft()设置一个全局标志,由questions()测试,如果设置使代码离开while (1)循环。

确保使用互斥锁保护对标志的访问。

谈论“受保护的访问”:key可以在没有保护的情况下同时访问。不好。


1
投票

这个例子使用pthread的功能来设置一个计时器并在出现问题时取消线程,我没有检查这个例子中的任何错误。在实际应用中,您必须这样做:

#include <stdio.h>
#include <pthread.h>
#include <time.h>

void *wrapper_handle_question(pthread_cond_t *cond) {
  char buf[2048];
  size_t i = fread(buf, 1, sizeof buf - 1, stdin);
  buf[i] = '\0';
  printf("%s", buf);
  pthread_cond_broadcast(cond);
  return NULL;
}

void *handle_question(void *arg) { return wrapper_handle_question(arg); }

int main(void) {
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  pthread_t question;
  pthread_create(&question, NULL, &handle_question, &cond);

  struct timespec ts;
  clock_gettime(CLOCK_REALTIME, &ts);
  ts.tv_sec += 5;

  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_mutex_lock(&mutex);

  int rc = pthread_cond_timedwait(&cond, &mutex, &ts);
  pthread_mutex_unlock(&mutex);
  if (rc == 0) {
    pthread_join(question, NULL);
  } else {
    pthread_cancel(question);
    printf("timeout!\n");
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.