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.
Printable View
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
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.
Sleeping puts a thread into sleep, which means it won't do anything for roughly the specified time interval. It will not consume any CPU.
When the interval has elapsed, the thread is re-awoken by the operating system and continues to do work.
thanks for ur replys
exactly!.Quote:
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.Quote:
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
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
No .But i have used threads and my above idea had been implemented using Qt(C++ library for building cross-platform GUI applications).Quote:
Have you used pthreads before?
Ah ok sorry guys I guess I got that wrong, thanks for correcting me. :)
Thanks for the advice.Is there any other idea for the problem.Actually for every 1000 millisecond i get the data from external source .Quote:
It may well "work" as long as your system is lightly loaded and allows your two threads to run most of the time, but it's certianly not a robust design
So I take it that the application waits on some sort of indication that data is available, right? And when you receive that indication, you copy the data to an array or some such.
If that's the case, your second thread should be waiting for a semaphore, then when your copying is finished, you signal that semaphore and that lets the second thread run.
This is pretty normal way of solving this sort of problem.
--
Mats
Thanks for u r advice . Actually i want to collect data from external source every 1000 milliseconds and store it in circular buffer.In the other thread the data is computed which is time consuming.Any suggestions would be a great help for me.Quote:
It may well "work" as long as your system is lightly loaded and allows your two threads to run most of the time, but it's certianly not a robust design.
Then you should use a os implemented queue or pipe to forward the data to the processing thread. You probably shouldn't try to implement your own queue using a circular buffer, as that tends to cause subtle bugs that only happen when you have the system in production. The processing thread should read from the queue/pipe.
If for some reason the queue/pipe implementation is relatively limited and you write lots of data, you could allocate memory buffers for a few blocks of data, and use a two-way communication where you free a buffer when it's processing is completed (perhaps using a third thread for this work), and only pass the address of the new data to the processing thread - that would prevent the overhead of writing lots of data an possibly blocking on the writing operation
--
Mats
Thanks for the idea.I understood the idea.But the problem is i dont know how to send data through queue and receive from queue.Are there any libraries to do that.
linux(RHEL4)
[babu@localhost aaff]$ uname -a
Linux localhost.localdomain 2.6.9-22.ELsmp #1 SMP Mon Sep 19 18:32:14 EDT 2005 i686 i686 i386 GNU/Linux
One of the first hits on google for "interthread communication" is this:
http://freshmeat.net/projects/itc/
Not sure if it's any good or not, but I'd expect it's better than rolling your own.
--
Mats
Thanks for the keyword "interthread communication" i will search more ,implement and then will post the problems.