独自の 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に関するものです。