Thread: Programming Timers

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    8

    Programming Timers

    Hi everyone...

    I am new in this forum... hope i can help someone anytime

    I have a problem.. i have a kind of structure, simple list structure and i need to have one timer for each node of the list...

    I wrote a program, where each node has a pthread associated with it, so, when I create and add a new node to the list, this thread starts to run... and it does!!!!

    I add a signal (sigalarm) and and used the setitimer..... so with this i catch the alarm signal and it works well, but just for one thread....

    Whe i add another thread, its timers run, but not the first thread's timer...looks like, i just can have one timer for the whole programm, and i need to have one timer for each thread....

    Can anyone help me?.... please.....

    If someone knows, other thing could help me is to get a way to interrupt a timer when it is running... i mean, pause, reset or stop a timer at any time....

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if the list is ordered by time, all you need is a timer for the head node of the list.

    Time expires, you run a thread for that node and you set the timer for the delta-time to the next node in the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    well, i think i did not explain myself well... as i wrote, i have a simple list structure... and i need to have one timer checking each node of the list... (one timer for each node) i create one pthread for each node... and i tryed to run a timer in each thread... it works for one node, but when i add a second one, this starts its thread and its timer, but the first node's timer doesn't

    Is like i cant have multiple timers.....

    I used the sigalarm, and i start the timer using setitimer function...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is like i cant have multiple timers.....
    Correct - read the manual, that's what it says.

    So either
    - simplify your design so it only depends on one timer
    - write your own clever wrapper around the existing timer functions to implement the features you desire.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Ok, thanks Salem...

    So, even i have many threads, and i add the signal function to each one "sinal(SIGALARM, funcion_handler )", each thread doesn't control the signals by its self, it is just one signal handler for the whole programm.... too bad for me ....

    Is there a way, that the signal handler notice from which thread is the signal comming from? i mean, in the signal handler function, i can check which thread's alarm has active the signal???

    Should I give up with this idea?

    Thanks again for answering!!!!!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is there a way, that the signal handler notice from which thread is the signal comming from?
    Probably not.
    Even if there were, it's possible that the alarm could be raised during a thread context switch. If you're between threads, which do you choose - the old one or the new one?

    Signal handlers happen asynchronously to the main execution path, which means you can't call most functions. This site lists all the POSIX functions you can call in a signal handler. Calling say printf() while a thread is in the middle of printf() could lead to all sorts of problems (very rare and very difficult to track down problems).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  2. Enumerating timers and hotkeys
    By maxorator in forum Windows Programming
    Replies: 3
    Last Post: 01-01-2007, 11:47 AM
  3. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 06:43 PM
  4. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM
  5. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM