Thread: To call a function repeatedly after certain period

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    32

    To call a function repeatedly after certain period

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    On Windows: Sleep() from Windows.h
    On *nix: usleep() from unistd.h

    Remember to RTM when it comes to platform-specific functions.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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:
    Code:
    void func()
    {
        cout << "Hello\n";
    }
    
    int main()
    {
       
       for(i = 0; i < 1000;i++)
       {
          Sleep(100);
          func();
       }
       return 0;
    }
    In Unix/linux, you could use the alarm() function:
    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.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Quote Originally Posted by matsp View Post
    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:
    Code:
    void func()
    {
        cout << "Hello\n";
    }
    
    int main()
    {
       
       for(i = 0; i < 1000;i++)
       {
          Sleep(100);
          func();
       }
       return 0;
    }
    In Unix/linux, you could use the alarm() function:
    http://linux.die.net/man/2/alarm
    or the interval timer (same thing, different API):
    http://linux.die.net/man/2/setitimer

    --
    Mats

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

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    thanks for ur replys

    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].

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by swgh View Post
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swgh View Post
    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.

    --
    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.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by babu198649 View Post
    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.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Have you used pthreads before?
    No .But i have used threads and my above idea had been implemented using Qt(C++ library for building cross-platform GUI applications).

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Ah ok sorry guys I guess I got that wrong, thanks for correcting me.
    Double Helix STL

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by babu198649 View Post
    No .But i have used threads and my above idea had been implemented using Qt(C++ library for building cross-platform GUI applications).
    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.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM