By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a

"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:cc**********@newsg2.svr.pol.co.uk...
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
Jul 22 '05 #3

P: n/a
PKH

"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:cc**********@newsg2.svr.pol.co.uk...
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
Jul 22 '05 #4

P: n/a
PKH wrote:
"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:cc**********@newsg2.svr.pol.co.uk...
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
Jul 22 '05 #5

P: n/a
Peter van Merkerk <me*****@deadspam.com> wrote in message news:<2k************@uni-berlin.de>...
[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 22 '05 #6

P: n/a
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.
Jul 22 '05 #7

P: n/a
pu*********@hotmail.com wrote:
Peter van Merkerk <me*****@deadspam.com> wrote in message news:<2k************@uni-berlin.de>...
[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
Jul 22 '05 #8

P: n/a
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.
Jul 22 '05 #9

P: n/a
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
Jul 22 '05 #10

P: n/a

"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:cc**********@newsg2.svr.pol.co.uk...
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.
Jul 22 '05 #11

P: n/a

"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:cc**********@newsg2.svr.pol.co.uk...
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
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.