how to introduce a small delay without using cpu time too much

Discussion in 'C++' started by Philip Parker, Jul 5, 2004.

  1. any ideas? sleep(1) is far too long a delay, im looking for something in the
    range of 50-100 milliseconds at most.
    something which doesnt suck up cpu time too much would be nice. is there
    anything in the standard cpp library which can do this?

    another thing. how can i make a static class function member , access things
    in an object of the same class?
    ie i have a class Bot . in it will be a static function ( needs to be static
    for multithreading :/ ) that needs to access a string array specific to the
    class object of it thats called. how can i access it?
     
    Philip Parker, Jul 5, 2004
    #1
    1. Advertising

  2. Philip Parker

    Ali Cehreli Guest

    On Mon, 05 Jul 2004 08:54:11 +0100, Philip Parker wrote:
    > another thing. how can i make a static class function member , access things
    > in an object of the same class?
    > ie i have a class Bot . in it will be a static function ( needs to be static
    > for multithreading :/ ) that needs to access a string array specific to the
    > class object of it thats called. how can i access it?


    Such functions have at least one 'void *' parameter to be used to pass-
    in any data that it needs to work on. You will need to cast that 'void
    *' to the actual type before using it:


    #include <iostream>

    struct S
    {
    int i_;

    static void Func(void * data)
    {
    S & s = *static_cast<S *>(data);
    s.i_ = 42;
    }
    };

    int main()
    {
    S s;
    S::Func(&s);
    std::cout << s.i_ << '\n';
    }

    Instead of passing only the object, you can pass a more elaborate
    type that carries more information:

    struct MoreData
    {
    S * s;
    OtherData od;
    };

    Then in the function:

    static void Func(void * data)
    {
    MoreData & moreData = *static_cast<MoreData *>(data);
    S & s = moreData.s;
    OtherData & otherData = moreData.od;

    /* use s and otherData here */
    }

    Ali
     
    Ali Cehreli, Jul 5, 2004
    #2
    1. Advertising

  3. "Philip Parker" <> wrote in message
    news:ccb1f3$gp3$...
    > any ideas? sleep(1) is far too long a delay, im looking for something in

    the
    > range of 50-100 milliseconds at most.
    > something which doesnt suck up cpu time too much would be nice. is there
    > anything in the standard cpp library which can do this?


    No there isn't.

    >
    > another thing. how can i make a static class function member , access

    things
    > in an object of the same class?
    > ie i have a class Bot . in it will be a static function ( needs to be

    static
    > for multithreading :/ ) that needs to access a string array specific to

    the
    > class object of it thats called. how can i access it?
    >


    Pass the object you want to access as a parameter to the static function.

    john
     
    John Harrison, Jul 5, 2004
    #3
  4. Philip Parker

    PKH Guest

    "Philip Parker" <> wrote in message
    news:ccb1f3$gp3$...
    > any ideas? sleep(1) is far too long a delay, im looking for something in

    the
    > range of 50-100 milliseconds at most.
    > something which doesnt suck up cpu time too much would be nice. is there
    > anything in the standard cpp library which can do this?
    >
    > another thing. how can i make a static class function member , access

    things
    > in an object of the same class?
    > ie i have a class Bot . in it will be a static function ( needs to be

    static
    > for multithreading :/ ) that needs to access a string array specific to

    the
    > class object of it thats called. how can i access it?
    >
    >


    Sleep(50) should be 50 milliseconds. Maybe you mean microseconds ?

    PKH
     
    PKH, Jul 5, 2004
    #4
  5. PKH wrote:

    > "Philip Parker" <> wrote in message
    > news:ccb1f3$gp3$...
    >
    >>any ideas? sleep(1) is far too long a delay, im looking for something in

    >
    > the
    >
    >>range of 50-100 milliseconds at most.
    >>something which doesnt suck up cpu time too much would be nice. is there
    >>anything in the standard cpp library which can do this?

    >
    > Sleep(50) should be 50 milliseconds. Maybe you mean microseconds ?


    That depends on the OS or library you are using; on MS-Windows your are
    correct, but on POSIX platforms the parameter passed to sleep()
    indicates the number of seconds to suspend the thread. Standard C++ does
    not have facilities to temporarilly suspend a threat, you need platform
    specific functions for that.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Jul 5, 2004
    #5
  6. Philip Parker

    Guest

    Peter van Merkerk <> wrote in message news:<>...
    [snip]
    > Standard C++ does
    > not have facilities to temporarilly suspend a threat, you need platform
    > specific functions for that.


    <tongueInCheek>
    I propose that the Freudian slip in this sentence be incorporated
    into the standard. A line of program execution should not be called
    a thread but a "threat" for the obvious danger it implies to data, etc.
    </tongueInCheek>
    Socks
     
    , Jul 5, 2004
    #6
  7. Philip Parker

    Julie Guest

    Philip Parker wrote:
    >
    > any ideas? sleep(1) is far too long a delay, im looking for something in the
    > range of 50-100 milliseconds at most.
    > something which doesnt suck up cpu time too much would be nice. is there
    > anything in the standard cpp library which can do this?


    There is no way to do this in C++. You need to resort to your operating
    system/hardware/thread package for an implementation specific way to do this.
     
    Julie, Jul 5, 2004
    #7
  8. wrote:
    > Peter van Merkerk <> wrote in message news:<>...
    > [snip]
    >
    >>Standard C++ does
    >>not have facilities to temporarilly suspend a threat, you need platform
    >>specific functions for that.

    >
    >
    > <tongueInCheek>
    > I propose that the Freudian slip in this sentence be incorporated
    > into the standard. A line of program execution should not be called
    > a thread but a "threat" for the obvious danger it implies to data, etc.
    > </tongueInCheek>


    threats...threads...for dyslectics like me it is the same. ;-)

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Jul 6, 2004
    #8
  9. Philip Parker

    Buster Guest

    Philip Parker wrote:
    > any ideas? sleep(1) is far too long a delay, im looking for something in the
    > range of 50-100 milliseconds at most.
    > something which doesnt suck up cpu time too much would be nice. is there
    > anything in the standard cpp library which can do this?
    >
    > another thing. how can i make a static class function member , access things
    > in an object of the same class?
    > ie i have a class Bot . in it will be a static function ( needs to be static
    > for multithreading :/ ) that needs to access a string array specific to the
    > class object of it thats called. how can i access it?


    This is impossible in standard C++. That said, on POSIX systems you can
    use select, like this:

    #include <sys/select.h>

    // ...

    timeval tv = { seconds, microseconds, };
    select (0, 0, 0, 0, & tv);

    --
    Regards,
    Buster.
     
    Buster, Jul 6, 2004
    #9
  10. Buster wrote:

    > Philip Parker wrote:
    >
    >> any ideas? sleep(1) is far too long a delay, im looking for something
    >> in the
    >> range of 50-100 milliseconds at most.
    >> something which doesnt suck up cpu time too much would be nice. is there
    >> anything in the standard cpp library which can do this?

    >
    > This is impossible in standard C++. That said, on POSIX systems you can
    > use select, like this:
    >
    > #include <sys/select.h>
    >
    > // ...
    >
    > timeval tv = { seconds, microseconds, };
    > select (0, 0, 0, 0, & tv);


    Or use usleep().

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Jul 6, 2004
    #10
  11. Philip Parker

    Xenos Guest

    "Philip Parker" <> wrote in message
    news:ccb1f3$gp3$...
    > any ideas? sleep(1) is far too long a delay, im looking for something in

    the
    > range of 50-100 milliseconds at most.
    > something which doesnt suck up cpu time too much would be nice. is there
    > anything in the standard cpp library which can do this?
    >

    No, nothing in the standard. If you need to wait a short time, shorter than
    the granularity of your system clock tick, but long enough that you have to
    relinquish the processor, the best way is a one-shot timer tied to an
    interrupt.
     
    Xenos, Jul 6, 2004
    #11
  12. "Philip Parker" <> wrote in message
    news:ccb1f3$gp3$...
    > any ideas? sleep(1) is far too long a delay, im looking for something in

    the
    > range of 50-100 milliseconds at most.
    > something which doesnt suck up cpu time too much would be nice. is there
    > anything in the standard cpp library which can do this?
    >


    I have to assume that sleep(1) on your system is for 1 second?

    Does sleep only take integral arguments? Have you tried sleep(0.05)?

    > another thing. how can i make a static class function member , access

    things
    > in an object of the same class?
    > ie i have a class Bot . in it will be a static function ( needs to be

    static
    > for multithreading :/ ) that needs to access a string array specific to

    the
    > class object of it thats called. how can i access it?
    >


    A static member function doesn't get "this", so it can't access anything
    that require knowledge of "this". The only way for it to know "this" is
    to pass it, either as a parameter, or as a global (and we *hate* globals).

    Most sensibly defined callback functions define the function prototype with
    a void * argument, that you can use to pass "this" or a struct with "this"
    in
    it. You can extract it (if necessary), cast it, and use it.

    Rufus
     
    Rufus V. Smith, Jul 7, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. priya
    Replies:
    3
    Views:
    1,127
    Ralf Hildebrandt
    Oct 8, 2005
  2. Perro Flaco
    Replies:
    1
    Views:
    359
    mlimber
    Oct 25, 2005
  3. cpp4ever
    Replies:
    3
    Views:
    398
    Francesco
    Sep 8, 2009
  4. Garrett Smith

    Too Much Processing (CPU)

    Garrett Smith, Oct 14, 2009, in forum: Java
    Replies:
    6
    Views:
    518
    Martin Gregorie
    Oct 24, 2009
  5. py
    Replies:
    1
    Views:
    378
Loading...

Share This Page