Thread: Threaded time event

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Threaded time event

    I need a new threaded timer event system. My current solution is not working as I would like it. This is the problem...

    CPlayer and CMonster fight. A new thread is launched in which time elapses. Once a certain time is reached Cmonster has to either perform an action or resort to the default action. Next is CPlayer. It then has also a few seconds to input an action or resort to the default one.

    The thread responsible for the timer is too heavy for the CPU because of my rather weak system for counting time which is based on an endless loop:

    Code:
    // something like this
    while(true) {
        if(time == threshold)
            break;
    }
    I need something better. I can't seem to find any info about this. Can you at least point me in the right direction for a more efficient time based counter? The system is Windows.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    630
    Did you consider using WaitForSingleObject?

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > Did you consider using WaitForSingleObject?

    Ah yes... forgot about that important piece of info... it's console based and I mean it to be portable across systems. No windows header or anything like that.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Threads aren't exactly portable, you know. Nor is there a portable of doing input with possible timeout.
    What are you doing right now to achieve this goal?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I was hoping for a more lower level solution that I could use to implement without the need to use system specific headers. If the solution is using these headers then I guess I have no choice.

    Threads are being developed using the boost::threads library. So, I'm not worried with that part. Console formatting is being done through NCurses, which does force some code changes between Windows, Linux (and MacOS). But not too much. So that is covered too. Seems this timer is the only thing I have for which there isn't a solution that can cover both Linux and Windows at least.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    I think boost::mutex has something that might be useful for you.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    2
    A call to sleep instead of the busy look ought to work better. The boost library, at least from their documentation, has a timer mutex that, upon timing out, should release its lock. If that is the case, you wait on the lock until it times out and effectively mimick the call to sleep. Another way is to use the platform's native timer. So, basically, you send the API a callback function and the API calls your function every interval. But to make this technique work good on both windows and linux may require some work. Also, if the sleep is done on the main thread, then you'll loose all interactivity for the time period, which in most cases is not good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  3. delegate & event
    By Micko in forum C# Programming
    Replies: 5
    Last Post: 03-08-2004, 04:05 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM