Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

using pthreads and callbacks

using pthreads and callbacks

using pthreads and callbacks



I have just created a simple program, as this is my first time with threads and callbacks.
However, 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;
    /* call the cb to inform the user time is out */

int main()
    pthread_t thread_id;
    int seconds = 3;
    int rc;

    rc =  pthread_create(&thread_id, NULL, g_start_timer, (void *) &seconds);
    printf("=== Failed to create thread\n");

    pthread_join(thread_id, NULL);

    printf("=== End of Program - all threads in ===\n");

    return 0;

RE: using pthreads and callbacks

working in inter-process comunication,
(probably with trhread is similar),
when a process "sleeps", you can send a signal()
with WAKE signal:
if the process does not receive signal, programs
continues after sleep statement;
if it receive signal WAKE or Ctrl-C (if it is running in console mode), the program jumps at signal (callback) routine, that you have declared.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close