Hello,
I am wondering where I should call my pthread_join().
I have a file called stopwatch.c. In that that file I create a thread and in that thread I pass a function that will sleep for a period of seconds. When the seconds have completed it will call a call back function.
I would like to implement this file in any of my programs. However, I would like the call back to be called in the main function, and not in the stopwatch_timer.c.
The reason this is, is because the main.c has some data that needs to be processed when the time expires. And the stopwatch_timer shouldn't have access this to this data.
Just one more question, am I passing my seconds correctly, as I seem to have incorrect data been sent. This is what I get.
=== g_start_timer(): Seconds: 7643124
Many thanks,
stopwatch_timer.c
Code:/* Implemenation for using a stopwatch timer */ #include <stdio.h> #include <pthread.h> /* function prototypes */ static void* g_start_timer(void *args); void timeout_cb(); /* call back function that is called when the time has expired */ void timeout_cb() { printf("=== Time has expired ===\n"); } /* start the stop watch in its own thread */ void start_stopwatch(int seconds) { pthread_t thread_id; int rc = 0; int secs = 3; printf("=== start_stopwatch(): %d\n", seconds); rc = pthread_create(&thread_id, NULL, g_start_timer, (void *) &secs); if(rc) printf("Error creating thread\n"); else printf("=== Success in creating thread\n"); } /* Start the timer in the thread */ static void* g_start_timer(void *args) { /* get the number of seconds and cast to int */ int seconds = *((int*) args); printf("=== g_start_timer(): Seconds: %d\n", seconds ); /* call back function */ void (*function_ptr)(); /* assign the address of the cb to the function pointer */ function_ptr = timeout_cb; /* sleep for a period of seconds specified */ sleep(3); /* call the call back function after time expired */ (*function_ptr)(); pthread_exit(NULL); }



LinkBack URL
About LinkBacks



