我已经编写了自己的stop_watch模块。这将创建一个线程并进入休眠状态一段时间。一旦秒数到期,它将调用main.c中的回调函数并通知用户时间已过期。

这样用户输入数字只需3秒钟,他们必须输入5位数字。如果时间到期,程序必须停止。

2个问题。 1)如果他们在所需时间内输入数字。我该如何取消该帖子。我在考虑使用thread_kill或thread_cancel? 2)如何终止do_while循环?因为scanf会在等待用户进入时阻塞。

非常感谢任何建议,

我的代码如下:

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

#include "stop_watch.h"

struct data_struct *g_data_struct;

void timeout_cb(int id)
{
    printf("Digit timeout\n");
    free(g_data_struct);
}

int main()
{
    pthread_t thread_id;
    unsigned int digit = 0;

    g_data_struct = (struct data_struct*) calloc(1, sizeof(*g_data_struct));

    if(!g_data_struct)
    {
        printf("=== failed to allocate memory ===\n");
        return 0;
    }

    /* start timer for 3 seconds */
    g_data_struct->seconds = 3;
    g_data_struct->func_ptr = timeout_cb;
    thread_id = start_stopwatch(g_data_struct);

    do
    {
        printf("Enter digit: ");
        scanf("%d", &digit);
    }while(1);

    pthread_join(thread_id, NULL);
    printf("=== End of Program - all threads in ===\n");

    free(g_data_struct);

    return 0;
}



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

#include "stop_watch.h"

pthread_t thread_id;
static id = 10;

/* start sleeping and call the callback when seconds have expired */
static void* g_start_timer(void *args)
{
    void (*function_pointer)(int id);
    int seconds = ((struct data_struct*) args)->seconds;
    function_pointer = ((struct data_struct*) args)->func_ptr;

    sleep(seconds);

    (void) (*function_pointer)(id);

    pthread_exit(NULL);

    return 0;
}

/* Will sleep in its own thread for a period of seconds */
int start_stopwatch(struct data_struct *g_data_struct)
{
    int rc = 0;

    int seconds = g_data_struct->seconds;
    printf("=== start_stopwatch(): %d\n", seconds);

    rc =  pthread_create(&thread_id, NULL, g_start_timer, (void *) g_data_struct);

    if(rc)
    {    
        printf("=== Failed to create thread\n");
        return 1;
    }

    return thread_id;
}

顺便说一句,这个问题是关于C99 gcc的。

有帮助吗?

解决方案

我不建议首先使用单独的线程。但是因为它看起来像是一个学习如何使用线程的练习,所以我会选择它。

您可以设置一个全局布尔标志,指示是否输入了数字。在g_start_timer中,在调用function_pointer之前,检查是否已设置该标志。如果是,请不要调用该函数。

如果您希望线程在睡眠期到期之前更早死亡,则可以使用带有try锁的互斥锁。

编辑:既然你说这不是一个练习,我会提到我会怎么做。使用在等待stdin输入时可能超时的函数,例如select()或poll()。或者,使用alarm()来中断。以下是使用警报的示例:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>

void alarm_signal(int signum)
{
}

int main()
{
    char c;
    unsigned int v = 0;
    struct sigaction act;
    puts("Enter digit: ");

    /* Register dummy handler for alarm */
    act.sa_handler = &alarm_signal;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGALRM, &act, NULL);

    /* Set timer for 3 seconds */
    alarm(3);

    /* Read the digits */
    errno = 0;
    while(0 != read(STDIN_FILENO, &c, 1))
    {
        if(c < '0' || c > '9')
            break;
        v = v*10 + c - '0';
    }
    if(errno == EINTR)
        puts("Timed out");
    else
        printf("Value is %d\n", v);
    return 0;
}

其他提示

当然你可以使用thread_cancel取消一个帖子。 至于scanf,您可能会考虑使用管道来模拟来自终端的输入。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top