To call a function repeatedly after certain period

This is a discussion on To call a function repeatedly after certain period within the C++ Programming forums, part of the General Programming Boards category; hi
I need to call a function repeatedly after some fixed time(100 milli seconds).
Which header files and classes should ...

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:

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.

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:

That's a pretty vague requirement, but it sounds like you want to spin off a separate thread to do this task

exactly!.

What exactly are you trying to do?

The basic idea is this.
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].

But surley calling a function 1000 times is going to overload the stack? Would it be better to call in recursivley?

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.

But surley calling a function 1000 times is going to overload the stack? Would it be better to call in recursivley?

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.

The basic idea is this.
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].

Well, I'd use a structure like a queue instead of a raw array. Have you used pthreads before? That's the first thing I'd tackle - getting both threads running and synchronized.

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.