Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to introduce a small delay without using cpu time too much

Reply
Thread Tools

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

 
 
Philip Parker
Guest
Posts: n/a
 
      07-05-2004
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?


 
Reply With Quote
 
 
 
 
Ali Cehreli
Guest
Posts: n/a
 
      07-05-2004
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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-05-2004

"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


 
Reply With Quote
 
PKH
Guest
Posts: n/a
 
      07-05-2004

"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


 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-05-2004
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
 
Reply With Quote
 
puppet_sock@hotmail.com
Guest
Posts: n/a
 
      07-05-2004
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
 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      07-05-2004
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.
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-06-2004
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
 
Reply With Quote
 
Buster
Guest
Posts: n/a
 
      07-06-2004
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.
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-06-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to introduce delay in Structural description ? priya VHDL 3 10-08-2005 06:51 AM
Append time to selected small date time field gil ASP General 1 04-16-2005 07:27 AM
Time delay Aliki VHDL 2 09-22-2004 02:20 PM
Re: time delay Roedy Green Java 1 07-21-2003 08:57 AM
Re: time delay Andy Flowers Java 0 07-20-2003 05:07 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57