Threads and Timers

This is a discussion on Threads and Timers within the C Programming forums, part of the General Programming Boards category; Well It is me again asking about threads hehehehe This time im interested in timers and threads. I've read about ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    Threads and Timers

    Well It is me again asking about threads hehehehe

    This time im interested in timers and threads. I've read about signals and I understand pretty much everything except pthread_sigmask() why is this function important when implementing timers with signals and alarm(). I am a bit lost with pthread_sigmask() Help


    Thanks in advance

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    pthread_sigmask() is a thread-safe version of sigprocmask(). You also shouldn't use signal() in a mutli-threaded app. Use sigaction() instead.

    gg

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,161
    Quote Originally Posted by scioner View Post
    Well It is me again asking about threads hehehehe

    This time im interested in timers and threads. I've read about signals and I understand pretty much everything except pthread_sigmask() why is this function important when implementing timers with signals and alarm(). I am a bit lost with pthread_sigmask() Help
    POSIX places a lot of restrictions on signals in multithreaded applications. For instance, the exact thread which receives a particular signal is undefined by the standard. In order to control signal delivery, only a single thread may unmask the desired signal, and it must remain masked in all other threads.

    On Linux, a thread and a process are basically the same thing, but on many UNIX systems they are not. On some systems, all signals might be intercepted by the pthreads library and dispatched through user space. In general you can't know for sure how exactly this dispatch operation happens. So to avoid mistakes, pthreads requires you to configure signals through pthreads-specific methods instead of the system's native methods.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    reply

    so basically threads and signals are a pain is there any other method to implement a timer I don't want to use select() or poll() I tried to use another thread but that does not go well with my design because of the mutexes and global variables.

    so any advice?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    It's not that bad. Use the example code in the following link as a template:
    http://www.opengroup.org/onlinepubs/...d_sigmask.html

    gg

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,161
    Quote Originally Posted by scioner View Post
    so basically threads and signals are a pain is there any other method to implement a timer I don't want to use select() or poll() I tried to use another thread but that does not go well with my design because of the mutexes and global variables.

    so any advice?
    All you have to do is use pthread_sigmask() to mask all signals in every thread you create. In other words, the first thing you do in every thread function is call pthread_sigmask() to mask everything. In the single thread where you want the timer signal to be delivered, call pthread_sigmask() again to unmask the signal and everything will be okay.

    But I'm kind of wondering why you're using a signal to implement a timer in a multithreaded application. Why not just have a timer thread that sleeps for the required duration? When it wakes up, it can indicate this to the rest of the program with a pthread_cond_broadcast().

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    >> In the single thread where you want the timer signal to be delivered, call pthread_sigmask() again to unmask the signal ...
    Well, the only way I know for one of your threads to handle a signal in its own context is to block that signal on all threads, then poll for the signal using sigwait(), sigtimedwait(), or sigwaitinfo(). This avoids some of the restrictions of what you can and can't do inside a signal handler.

    >> Why not just have a timer thread that sleeps for the required duration?
    That's certainly doable. I suppose using signal based timers makes sense if you've got other signals that could also be handled in the thread - or if you have multiple timers.

    gg

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    14
    ll you have to do is use pthread_sigmask() to mask all signals in every thread you create. In other words, the first thing you do in every thread function is call pthread_sigmask() to mask everything.
    Doesnt threads inherit the signal masks? can I mask all the signals before creating a thread and then inside the thread unblock the alarma signal?

    About using pthread_cond_broadcast() that would certainly be a choice I just want to use signals see how it goes first to learn a little bit more about them

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    >> Doesnt threads inherit the signal masks? can I mask all the signals before creating a thread and then inside the thread unblock the alarma signal?
    Yes. But you leave any signals you want your thread to handle blocked (like in the example code). That way when the signal occurs, it will stay pending until "delivered" by sigwait().

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming Timers
    By jpablo in forum C Programming
    Replies: 5
    Last Post: 04-09-2006, 12:53 AM
  2. Stopping threads in DLLs
    By MWP in forum Windows Programming
    Replies: 7
    Last Post: 12-24-2005, 04:19 PM
  3. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 03:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21