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; It may well "work" as long as your system is lightly loaded and allows your two threads to run most ...

  1. #16
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    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
    Thanks for the advice.Is there any other idea for the problem.Actually for every 1000 millisecond i get the data from external source .

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by babu198649 View Post
    Thanks for the advice.Is there any other idea for the problem.Actually for every 1000 millisecond i get the data from external source .
    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
    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.

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

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by babu198649 View Post
    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.
    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
    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. #20
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    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.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by babu198649 View Post
    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.
    What OS are you using?

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

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

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

  9. #24
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Thanks for the keyword "interthread communication" i will search more ,implement and then will post the problems.

Page 2 of 2 FirstFirst 12
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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21