Find the answer to your Linux question:
Results 1 to 5 of 5
Hi everyone, i'm new in this forum, sorry for my English i've this question: I need programmer a timer of 10 ms for read/write a USB device, i hope you ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    1

    Creating a Timer

    Hi everyone, i'm new in this forum, sorry for my English i've this question:

    I need programmer a timer of 10 ms for read/write a USB device, i hope you can help me.

  2. #2
    Just Joined!
    Join Date
    Sep 2010
    Location
    Montgomery, AL
    Posts
    27
    Quote Originally Posted by david1820 View Post
    Hi everyone, i'm new in this forum, sorry for my English i've this question:

    I need programmer a timer of 10 ms for read/write a USB device, i hope you can help me.
    if you need it to run asynchronously,

    Create a thread using a method like the following.

    Code:
    static void Timer_Main()
    {
        while(continue)
       {      
         //call a sleep function for the amount of milliseconds you need
        //call a function pointer here executing a method you want run on timer elapse.
       }
    }
    I recommend using a Thread Class to encapsulate your thread and to make it object oriented.

    If it doesn't need to be asynchronous, just put an infinite loop in your program calling a certain function, then have the program sleep.

    I would write out some actual code for you, but that would take all of the fun away. Plus 10 minutes with google will show you how to use pthread or the like, and googling "Thread Class for pthreads" would also probably give you the general idea in the top 5 search results. I hope this gets you going in the right direction. Let me know if you need more help.

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Location
    Montgomery, AL
    Posts
    27
    Quote Originally Posted by david1820 View Post
    Hi everyone, i'm new in this forum, sorry for my English i've this question:

    I need programmer a timer of 10 ms for read/write a USB device, i hope you can help me.
    Here is a more elaborate example:


    First search result for "linux c++ timer"

    c++ - timer class in linux - Stack Overflow

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Since the USB device should be represented and bound to a file descriptor, then you can use select() with a timer to deal with device I/O from user space. I do not recommend JHenson's advice about creating a separate thread to do this. Once you get into a threaded environment, your program complexity goes up exponentially. Remember the KISS principle - Keep It Short and Simple (or Keep It Simple, Stupid!)
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Sep 2010
    Location
    Montgomery, AL
    Posts
    27
    Quote Originally Posted by Rubberman View Post
    Since the USB device should be represented and bound to a file descriptor, then you can use select() with a timer to deal with device I/O from user space. I do not recommend JHenson's advice about creating a separate thread to do this. Once you get into a threaded environment, your program complexity goes up exponentially. Remember the KISS principle - Keep It Short and Simple (or Keep It Simple, Stupid!)
    This is sound advice. I had forgotten about the select call.

    As to multi-threaded, it depends on what you need. While I agree that an extra thread shouldn't be used unless absolutely necessary, I can think of several designs where a separate signaling thread may be quite useful (usually a GUI is involved). I don't know what your design is, but if I am not mistaken, select will block so, if you need your program to continue (i.e. perform other tasks) during the block until it receives usb input, then you will need multiple threads. Otherwise, do not use another thread. If you are following the unix design practice of having a program that does one and only one task and does it well, then I completely agree with Rubberman.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...