473,748 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer in C (like in Java)

Hello, I need a facility in C, like a set of functions, to manage a
timer: I should be able to initialize it with a certain period of time
(like x msec), start it, and every x msec it should execute a certain
function. In a few words, I need something like the java.util.Timer
class, but for C (of course not a class).
Do you know of any implementation of something like that (if possible
real-time)?
Thanks a lot
Jun 27 '08
14 3025
Antoninus Twink wrote:
On 23 Apr 2008 at 22:34, Keith Thompson wrote:
>ro******@ibd.nr c-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.

Jun 27 '08 #11
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.

Jun 27 '08 #12
On 24 Apr, 22:08, Antoninus Twink <nos...@nospam. invalidwrote:
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
Jun 27 '08 #13
Alexander Mahone wrote:
On 24 Apr, 22:08, Antoninus Twink <nos...@nospam. invalidwrote:
>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 .programmerwher e you will surely
receive much better responses than here. Standard C has no facilities
for creating timers.

Jun 27 '08 #14
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/).

Jun 27 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
11110
by: Geoff | last post by:
Hey.. I'm having problems with stopping this timer from outside the 'runTimer()' class.. i have no problems calling 'cancel()' from inside the run() method but I want to be able to stop the Timer when I press a button (stopTimer()). With the code I've got here, i get an error saying 'void cannot be dereferenced' ... has anyone got any ideas as to what I'm doing wrong or how i could stop the timer by pressing the button? Any help would...
3
319
by: Mr. B | last post by:
My current app has a timer that I kick ON in my Form1_Load as follows: ' Set Up the Timer Function Dim t As New System.Timers.Timer(12000) ' 1000 = 1 Second t.Enabled = True ' False to Turn OFF AddHandler t.Elapsed, AddressOf TimerFired This in turns fires up my sub called "TimerFired" every 2 minutes... In this sub, I check a file for a change in the date/time and if I find a
0
1172
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it never fires off the sub) I've tried using java, I've tried using the system timer - this is my last hope - please help! Here's my Code ----------------- Private strConn As String = ConfigurationSettings.AppSettings("conString") Private Sub...
8
2172
by: bearophileHUGS | last post by:
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. ------------------- I am still ignorant about Tkinter. This little program, after pressing the "Go" eats more and more RAM, is it normal? Can it be avoided? (In normal programs this is isn't a real problem). ! import Tkinter
3
1774
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it never fires off the sub) I've tried using java, I've tried using the system timer - this is my last hope - please help! Here's my Code ----------------- Private strConn As String = ConfigurationSettings.AppSettings("conString") Private Sub...
1
1968
by: enzoJava | last post by:
Hii, I have this program and i need help on printing out the total time, can anyone help me on this? If there is a better way to do this let me know Thnx import javax.swing.*; import javax.swing.Timer; import java.awt.event.*;
4
5446
by: puntino | last post by:
Hi I have created my Alarm, at compile time I don't have any problem, but at run time I receive the follo wing messages: Exception in thread "main" java.lang.NullPointerException at java.util.Timer.sched(Unknown Source) at java.util.Timer.scheduleAtFixedRate(Unknown Source) at Alarm.AlarmExecute(Alarm.java:20) at Test.main(Test.java:11) (With <- I have pointed out the line 11 of test.java and line 20 of alarm.java)
8
2347
by: sip.address | last post by:
Hello, I'm trying to find some existing (and simple if possible) timer queue implementation. Does anybody know a simple skeleton to use as example? I just need to send simple (relative) timeouts. Thought about some possibilities but would prefer to use something already tested. Thanksç
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.