Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

using pthread to create a stop watch timer

Posted on 2009-02-23
5
Medium Priority
?
2,518 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:steve1_rm
  • 3
  • 2
5 Comments
 

Author Comment

by:steve1_rm
ID: 23714072
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;
}

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 150 total points
ID: 23719940
What you need is a pthread_join call to let the main code wait for the timer thread to finish :
#include <stdio.h>
#include "stopwatch_timer.h"
int main()
{
    printf("Start your timers\n");
    
    /* start the time and run for 10 seconds */
    pthread_t thread_id = start_timer(10);
 
    int i = 0; 
    /* do some work */
    for(i = 0; i < 1000; i++){}
 
    pthread_join(thread_id, 0);
 
    /* 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 */
void* g_start_timer(void *secs);
 
static clock_t _current_time = 0;
 
/* create the thread */
pthread_t 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");
    }
    
    return thread_id;
}
 
/* start the timing in another thread */
void* 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 */
#include <pthread.h>
pthread_t start_timer(int seconds);
int current_time();

Open in new window

0
 

Author Comment

by:steve1_rm
ID: 23724686
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

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23724734
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23731020
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question