Thread: Non blocking thread TIMER

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    Non blocking thread TIMER

    Hi all,

    I'm trying to do some timer thread but i have some trouble to do this, because i use sleep and it is a blocking call, so i need some more flexible function that do the job and don't block other threads. my code is :

    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    /* call back function - inform the user the time has expired */
    void timeout_call_back()
    {
        printf("=== your time is up ===\n");
    	// doing some other stuff
    }
    
    /* Go to sleep for a period of seconds */
    static void* start_timer(void *args)
    {
        /* function pointer */
        void (*finish_function)();
    
        
        int seconds = *((int*) args);
        pthread_mutex_lock(&mutex); // I want to do this action atomically
        printf("thread ID : %ld, go to sleep for %d\n", pthread_self(), seconds);
        
        finish_function = timeout_call_back;
        
        sleep(seconds); // THIS IS MY PROBLEM I WANT TO WAIT for (seconds) seconds but i don't want to block other threads
    
        /* call the cb to inform the user time is out */
        (*finish_function)();
        pthread_mutex_unlock(&mutex);
    	
        pthread_exit(NULL);
    }
    
    int main()
    {
        pthread_t thread1, thread2, thread3;
        int seconds1 = 300;
        int seconds2 = 600;
        int seconds3 = 900;
    
        int rc1, rc2, rc3;
    
        rc1 =  pthread_create(&thread1, NULL, start_timer, (void *) &seconds1);
        if(rc1)
    	printf("=== Failed to create thread1\n");
        rc2 =  pthread_create(&thread2, NULL, start_timer, (void *) &seconds2);
        if(rc2)
    	printf("=== Failed to create thread2\n");
    
        rc3 =  pthread_create(&thread3, NULL, start_timer, (void *) &seconds3);
        if(rc3)
    	printf("=== Failed to create thread3\n");
    
        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);
        pthread_join(thread3, NULL);
    
        printf("=== End of all threads in ===\n");
    
        return 0;
    }
    Thanks for your help.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well, i don't know if there's a way to get past this, but you should think of the alternative: Reduce the thread's priority instead calling sleep;
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Thanks "Sipher" what you mean by reducing priority? May be i'm wrong instead of use sleep to launch the timer ther's other tips to do that?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Every running process takes a time slice from the OS. If you lower its priority, it will take less than the average time slice, and in effect it won't make CPU so busy. I don't know how to implement it though, i'm a bit rusty on threads.
    Devoted my life to programming...

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I don't recommend changing thread priorities. 99% of the time it's best to let the OS handle all the scheduling.

    You could use timer_create() with a SIGEV_THREAD notification.

    gg

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Thanks for timer_create(), how can i deal with this function tin my case?

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I'm not sure what your case is...what do you want to accomplish with these timers?

    gg

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Thanks for the reply "Codeplug",

    When you run my code, you can see that the
    Code:
    sleep(seconds)
    inside the
    Code:
    start_timer(void *args)
    function blocks other threads until she terminates (This code is atomic), and i want threads to run simultaneously.

    Thanks

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by evariste View Post
    Hi all,

    I'm trying to do some timer thread but i have some trouble to do this, because i use sleep and it is a blocking call, so i need some more flexible function that do the job and don't block other threads. my code is :
    Are you sure it not your mutex lock that's causing you problem. If a thread locks a
    mutex and then sleeps holding the lock that will block the other threads waiting
    for the lock to free.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Thanks "kona49er",

    I said before that i want this part to be atomic and at the same time launch the timer inside this step, so i know that my problem is (sleep + mutex), but i don't know how to do this :

    1. create a timer thread, start it as non blocking and be able to test from other functions if my timer has expired and finally at expiration run "timeout_call_back" function.

    If you have an idea on how to do this feel free to answer me.

    Thanks.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ... be able to test from other functions if my timer has expired and finally at expiration run "timeout_call_back" function.
    Use timer_create() and timer_settime(). Use the SIGEV_THREAD notification type.
    Code:
    /* this is called "as-if" it were a new thread */
    void timeout_call_back(union sigval)
    {
    ...
    }
    
    ...
    sev.sigev_notify = SIGEV_THREAD;
    sev.sigev_value.sival_ptr = &timerid; /* sigev_value is passed to our callback */
    sev.sigev_notify_function = &timeout_call_back;
    sev.sigev_notify_attributes = NULL;
    https://www.kernel.org/doc/man-pages...igevent.7.html

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  2. Thread confusion
    By pyrolink in forum C Programming
    Replies: 0
    Last Post: 01-29-2006, 09:42 PM
  3. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  4. Replies: 12
    Last Post: 05-17-2003, 05:58 AM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM