Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 147461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:47:09+00:00 2026-05-11T08:47:09+00:00

I have written my own stop_watch module. That will create a thread and go

  • 0

I have written my own stop_watch module. That will create a thread and go to sleep for a period of seconds. Once the seconds have expired it will call a callback function in the main.c and inform the user the time has expired.

This is so that the user will only have 3 seconds to enter a digit and they will have to enter 5 digits. If the time expires the program has to stop.

2 problems. 1) if they enter the digit in the required time. How can I cancel the thread. I was thinking of using thread_kill or thread_cancel? 2) How can I terminate the in the do_while loop? As the scanf will block while waiting for the user to enter.

Many thanks for any suggestions,

My code below:

#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; } 

This question is about C99 gcc, by the way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:47:09+00:00Added an answer on May 11, 2026 at 8:47 am

    I wouldn’t recommend using a separate thread for this in the first place. But since it looks like an exercise to learn how to use threads, I’ll go with it.

    You can set a global boolean flag that indicates whether a digit was entered. In g_start_timer, just before calling function_pointer, check whether the flag has been set. If it was, don’t call the function.

    If you want the thread to die earlier, before its sleep period has expired, you can use a mutex with a try lock.

    EDIT: Since you say it’s not an exercise, I’ll mention how I would do it. Either use a function that can time out while waiting for input on a stdin, such as select() or poll(). Or, use alarm() to interrupt. Here’s an example of using 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; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer While hardly the simplest approach, but since this is a… May 11, 2026 at 2:39 pm
  • added an answer Since you are binding by code, the event associated with… May 11, 2026 at 2:39 pm
  • added an answer Like so (the 'select' is just the way of specifying… May 11, 2026 at 2:39 pm

Related Questions

I've just inherited a website (ASP.Net 2.0) written by someone else that I need
I have a large dataset (over 100,000 records) that I wish to load into
I have an application that allows users to write their own code in a
Is there any JavaScript library that makes a dictionary out of the query string,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.