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

P

Philip Parker

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?
 
A

Ali Cehreli

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
 
J

John Harrison

Philip Parker said:
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
 
P

PKH

Philip Parker said:
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
 
P

Peter van Merkerk

PKH said:
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.
 
P

puppet_sock

Peter van Merkerk said:
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
 
J

Julie

Philip said:
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.
 
P

Peter van Merkerk

<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. ;-)
 
B

Buster

Philip said:
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);
 
P

Peter van Merkerk

Buster said:
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().
 
X

Xenos

Philip Parker said:
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.
 
R

Rufus V. Smith

Philip Parker said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top