hi
I need to call a function repeatedly after some fixed time(100 milli seconds).
Which header files and classes should be used to do this.
hi
I need to call a function repeatedly after some fixed time(100 milli seconds).
Which header files and classes should be used to do this.
On Windows: Sleep() from Windows.h
On *nix: usleep() from unistd.h
Remember to RTM when it comes to platform-specific functions.
That's a pretty vague requirement, but it sounds like you want to spin off a separate thread to do this task, otherwise it could get delayed if something else is taking too long. What exactly are you trying to do?
There is no generic way to do that with standard C or C++. There may be C++ libraries available to support portable code to do it, but I don't know of one that will do just that without lots of extras (for example xWidgets which is a GUI package would most likely allow you to do something like that).
I'm not sure how you would go about it in Windows if you want it to be truly asynchrnous, but this would work if you don't need to do something else in the meantime:
In Unix/linux, you could use the alarm() function:Code:void func() { cout << "Hello\n"; } int main() { for(i = 0; i < 1000;i++) { Sleep(100); func(); } return 0; }
http://linux.die.net/man/2/alarm
or the interval timer (same thing, different API):
http://linux.die.net/man/2/setitimer
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
thanks for reply MacGyver,
i am using Linux(RHEL4).i want to use threads in my program .Does using usleep will stop the threads to continue.
thanks for ur replys
exactly!.That's a pretty vague requirement, but it sounds like you want to spin off a separate thread to do this task
The basic idea is this.What exactly are you trying to do?
i wnt to make a multithreaded program(consists of two threads).One Thread will fill the array every 1000milliseconds and the other thread will read the array after the array is filled by the first thread[i want to do it in linux].
You have that entirely backwards. Calling a function in a loop is perfectly fine since the stack will be in the same state each time (assuming the compiler doesn't pull any advanced optimizations of sorts). Calling a function recursively adds to the stack each time the function is called.
Eh? Calling recursively is what would build up stackframes (although we could probably do this in a way that the compile translates tail recursion into a loop - but then we would end up with the equivalent of what we're seeing here).
Note that if func() is taking a long time, then the 100ms would have to be reduced to make the time between one call to func and the next call to func be 100ms - you could write some code that adapts the time if it varies, too.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
medievalelks is absolutely right - you should not user timers or sleep (or usleep) for this purpose. You need proper synchronization between the threads, or you will end up getting out of sync sooner or later.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
No .But i have used threads and my above idea had been implemented using Qt(C++ library for building cross-platform GUI applications).Have you used pthreads before?
Ah ok sorry guys I guess I got that wrong, thanks for correcting me.
Double Helix STL
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.