Link to home
Start Free TrialLog in
Avatar of steve1_rm
steve1_rm

asked on

using pthread to create a stop watch timer

Hello,

I implemented my own stopwatch timer. I want to run this for a specific time. And then check what the current time as specific intervals in my code. However, I will run the start timer in another thread using pthread.

However, when I run the application it doesn't run for 10 seconds as it should. And it doesn't seem to go into the g_start_timer.

The value I always get returned is: 0. Which cannot be correct. Can anyone see any improvement in my code, as I have done something wrong.

many thanks,
#include <stdio.h>
#include "stopwatch_timer.h"
int main()
{
    printf("Start your timers\n");
    
    /* start the time and run for 10 seconds */
    start_timer(10);
 
    int i = 0; 
    /* do some work */
    for(i = 0; i < 1000; i++){}
 
    /* Check and display current time */
    printf("Curent time: %d\n", current_time());
 
    return 0;
}
 
 
 
/* stopwatch_timer.c */
#include <pthread.h>
#include <time.h>
#include <stdio.h>
 
#include "stopwatch_timer.h"
 
/* prototypes */
int* g_start_timer(void *secs);
 
static clock_t _current_time = 0;
 
/* create the thread */
void start_timer(int seconds)
{
    pthread_t thread_id;
    int rc = 0;
 
    rc = pthread_create(&thread_id, NULL, g_start_timer, (void*) seconds);
 
    if(rc)
    {
	printf("=== Error Creating thread\n");
    } 
}
 
/* start the timing in another thread */
int* g_start_timer(void *secs)
{
    printf("Starting thread\n");
    int seconds = (int) secs;
    printf("g_start_timer: %d\n", (int) seconds);
  
  _current_time = clock() + seconds * CLOCKS_PER_SEC;
 
    /* loop until the 10 seconds has reached */
    while(clock() < _current_time){}
 
    pthread_exit(NULL);
}
 
/* get the current time of work */
int current_time()
{
    return (int) _current_time / CLOCKS_PER_SEC;
}
 
 
 
/* stopwatch_timer.h */
void start_timer(int seconds);
int current_time();

Open in new window

Avatar of steve1_rm
steve1_rm

ASKER

hello,

I have changed my main to add the pthread_exit(NULL)

It now runs for 10 seconds.

However, I would have thought I wouldn't have managed to get the current time. Is this possible with what I am trying to do.

I thought with running in another thread. The for loop would act like it is doing some work. So when I check the time. I can get the current time which the g_start_timer function is up to.

Code:
int main()
{
    printf("Start your timers\n");
   
    /* start the time and run for 10 seconds */
    start_timer(10);

    int i = 0;
    /* do some work */
    for(i = 0; i < 100000; i++){}

    /* Check and display current time */
    printf("Curent time: %d\n", current_time());

    pthread_exit(NULL);

    return 0;
}

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,

I have just created another simple program using threads and callbacks, as this is my first time with threads and callbacks, I have a few questions.

I am wondering do I really need a callback. Can I just call the timeout_cb() function
after the sleep() as completed? Maybe I don't understand, why a callback is needed at all?

Just one more question. The time could be set for a long as 30 seconds. However, the task might
complete before then. Is there any way to stop the timer. As if the task has been completed before
the 30 seconds, I don't want the call back to be called at all? What is I need is stop_timer.

I was thinking is there anyway to stop the sleep once it has started?

Many thanks for any suggestions,



#include <pthread.h>
#include <stdio.h>
 
/* call back function - inform the user the time has expired */
void timeout_cb()
{
    printf("=== your time is up ===\n");
}
 
/* Go to sleep for a period of seconds */
static void* g_start_timer(void *args)
{
    /* function pointer */
    void (*function_pointer)();
 
    /* cast the seconds passed in to int and 
     * set this for the period to wait */
    int seconds = *((int*) args);
    printf("go to sleep for %d\n", seconds);
    
    /* assign the address of the cb to the function pointer */
    function_pointer = timeout_cb;
    
    sleep(seconds);
    /* call the cb to inform the user time is out */
    (*function_pointer)();
	
    pthread_exit(NULL);
}
 
int main()
{
    pthread_t thread_id;
    int seconds = 3;
    int rc;
 
    rc =  pthread_create(&thread_id, NULL, g_start_timer, (void *) &seconds);
    if(rc)
	printf("=== Failed to create thread\n");
 
    pthread_join(thread_id, NULL);
 
    printf("=== End of Program - all threads in ===\n");
 
    return 0;
}

Open in new window

These are different questions, not directly related to the original question. In order to keep things clean, please open a new question for them.

If the original question has been answered, then you can close this question.
May I ask why you gave a B grade ? That usually means that something was missing in the answer and/or that something is still unclear. If that's the case, then please do not hesitate to ask for clarification where needed.