473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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
11 1768
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

"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
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
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
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
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
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
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Brueck | last post by:
Below is some information I collected from a *small* project in which I wrote a Python version of a Java application. I share this info only as a data point (rather than trying to say this data...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
11
by: Maheshkumar.R | last post by:
Hi groups, How i can introduce some milliseconds delay in application. How i can do achieve this... let me clearly say... (1) I'm displaying slices of medical images. For framerate - for...
7
by: mfeingold | last post by:
I am working on a system, which among other things includes a server and a ..net control sitting in an html page and connected to the server. I ran into a couple of problems, you guys might have...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
0
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.