Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Timer in C (like in Java)

Reply
Thread Tools

Timer in C (like in Java)

 
 
santosh
Guest
Posts: n/a
 
      04-24-2008
Antoninus Twink wrote:

> On 23 Apr 2008 at 22:34, Keith Thompson wrote:
>> http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
>>> sleep() is not part of standard C. It is a common operating system
>>> extension, but the original poster did not specify an OS.

>>
>> Furthermore, it's defined differently on different systems. On POSIX
>> systems, for example, the argument is a number of seconds.

>
> You're right, but there are also usleep and nanosleep, for shorter
> intervals.


I think the OP wants something that will interrupt the program
periodically. I think some versions of nanosleep will do this, but as
Walter Roberson has rightly observed, the OP is better of consulting in
a platform specific group. His best bet might be setitimer/getitimer or
the timer_* group of functions.

 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      04-24-2008
On 24 Apr 2008 at 19:17, santosh wrote:
> Antoninus Twink wrote:
>> You're right, but there are also usleep and nanosleep, for shorter
>> intervals.

>
> I think the OP wants something that will interrupt the program
> periodically. I think some versions of nanosleep will do this

[snip]
> His best bet might be setitimer/getitimer or the timer_* group of
> functions.


Oops, sorry, I lost sight of what the OP had asked amidst all the
bluster.

Yes, setitimer should be just the ticket.

 
Reply With Quote
 
 
 
 
Alexander Mahone
Guest
Posts: n/a
 
      04-28-2008
On 24 Apr, 22:08, Antoninus Twink <(E-Mail Removed)> wrote:
> On 24 Apr 2008 at 19:17, santosh wrote:
>
> > Antoninus Twink wrote:
> >> You're right, but there are also usleep and nanosleep, for shorter
> >> intervals.

>
> > I think the OP wants something that will interrupt the program
> > periodically. I think some versions of nanosleep will do this

> [snip]
> > His best bet might be setitimer/getitimer or the timer_* group of
> > functions.

>
> Oops, sorry, I lost sight of what the OP had asked amidst all the
> bluster.
>
> Yes, setitimer should be just the ticket.


OK, I read various documentation on the Internet, but I think neither
ualarm() or setitimer() satisfy me completely. What I need to do, in
fact, is create a series of different timers, like timer objects, each
one referrable, with some kind of reference (a pointer or any other
way to refer to it in the future). Then, under certain conditions, I
need to reset one or more of them (that's why I need some way to refer
to every single timer specificly), to prevent them sending the
SIGALARM.
From what I've read, both ualarm() and setitimer() (the first of based
on the second) allows you to manage one single global timer, the one
of the caller process...
So, in my opinion (correct me if I'm wrong, my knownledge of C is
quite limited) the only 2 ways to do this would be:
1 - Create a series of threads, one for each timer I want to create,
and inside each thread call ualarm()...Or maybe I'd need to create
child processes, instead of threads?
2 - Use a series of timer_create(), without the need to create any per-
timer thread...
Is there a simpler and more lightweight way to do this?
Thanks a lot
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      04-28-2008
Alexander Mahone wrote:

> On 24 Apr, 22:08, Antoninus Twink <(E-Mail Removed)> wrote:
>> On 24 Apr 2008 at 19:17, santosh wrote:
>>
>> > Antoninus Twink wrote:
>> >> You're right, but there are also usleep and nanosleep, for shorter
>> >> intervals.

>>
>> > I think the OP wants something that will interrupt the program
>> > periodically. I think some versions of nanosleep will do this

>> [snip]
>> > His best bet might be setitimer/getitimer or the timer_* group of
>> > functions.

>>
>> Oops, sorry, I lost sight of what the OP had asked amidst all the
>> bluster.
>>
>> Yes, setitimer should be just the ticket.

>
> OK, I read various documentation on the Internet, but I think neither
> ualarm() or setitimer() satisfy me completely. What I need to do, in
> fact, is create a series of different timers, like timer objects, each
> one referrable, with some kind of reference (a pointer or any other
> way to refer to it in the future). Then, under certain conditions, I
> need to reset one or more of them (that's why I need some way to refer
> to every single timer specificly), to prevent them sending the
> SIGALARM.
> From what I've read, both ualarm() and setitimer() (the first of based
> on the second) allows you to manage one single global timer, the one
> of the caller process...
> So, in my opinion (correct me if I'm wrong, my knownledge of C is
> quite limited) the only 2 ways to do this would be:
> 1 - Create a series of threads, one for each timer I want to create,
> and inside each thread call ualarm()...Or maybe I'd need to create
> child processes, instead of threads?
> 2 - Use a series of timer_create(), without the need to create any
> per- timer thread...
> Is there a simpler and more lightweight way to do this?
> Thanks a lot


Please ask this <news:comp.unix.programmer> where you will surely
receive much better responses than here. Standard C has no facilities
for creating timers.

 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      04-28-2008
On 28 Apr 2008 at 13:21, Alexander Mahone wrote:
> OK, I read various documentation on the Internet, but I think neither
> ualarm() or setitimer() satisfy me completely. What I need to do, in
> fact, is create a series of different timers, like timer objects, each
> one referrable, with some kind of reference (a pointer or any other
> way to refer to it in the future). Then, under certain conditions, I
> need to reset one or more of them (that's why I need some way to refer
> to every single timer specificly), to prevent them sending the
> SIGALARM.
> From what I've read, both ualarm() and setitimer() (the first of based
> on the second) allows you to manage one single global timer, the one
> of the caller process...


Well, you can squeeze out three, producing the different signals
SIGALRM, SIGVTALRM and SIGPROF, but yes, there's a strictly limited
number of timers available to each process.

The usual way around this is to implement multiple timers by hand: set
up an ordered queue of timers, and as one expires initialize the next
one along.

Of course, that's a pain, and so people have already done it and put it
into libraries for you to use - a good one is libevent
(http://monkey.org/~provos/libevent/).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I know about the shutdown timer, is there a "switch on" timer... Mal Carter Computer Support 3 01-12-2008 01:58 AM
page timer, or redirect timer Simon ASP .Net 1 11-04-2005 07:14 PM
System.Timers.Timer/System.Threading.Timer Not Firing At All on Dev Server james.e.coleman@gmail.com ASP .Net 1 02-22-2005 09:41 PM
old timer on net but Mozilla newbie carlton hunter Firefox 1 01-31-2004 04:53 AM
System.Timers.Timer vs. System.Threading.Timer Kelsang Wangchuk ASP .Net 0 07-31-2003 04:28 PM



Advertisments