如何在不使用cpu时间的情况下引入小延迟 [英] how to introduce a small delay without using cpu time too much

查看:47
本文介绍了如何在不使用cpu时间的情况下引入小延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法?睡觉(1)是太长的延迟,我在最多50-100毫秒的

范围内寻找一些东西。

也不会吸收cpu时间的东西非常好。在标准的cpp库里有什么东西可以做到这一点吗?


另外一件事。如何创建一个静态类函数成员,在同一个类的对象中访问


即我有一个类Bot。在它中将是一个静态函数(需要是静态的多线程
:/)需要访问特定于其调用的

类对象的字符串数组。我如何访问它?

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?

推荐答案

2004年7月5日星期一08:54:11 + 0100,Philip Parker写道:
On Mon, 05 Jul 2004 08:54:11 +0100, Philip Parker wrote:
另一件事。我如何创建一个静态类函数成员,访问同一类对象中的东西?
即我有一个类Bot。在它中将是一个静态函数(需要是多线程的静态函数:/),需要访问特定于其调用的类对象的字符串数组。我如何访问它?
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?




这些函数至少有一个''void *''参数用于传递 -

in它需要处理的任何数据。在使用之前,您需要将'void

*''投射到实际类型:

#include< iostream>


struct S

{

int i_;


static void Func(void * data)

{

S& s = * static_cast< S *>(数据);

s.i_ = 42;

}

};


int main()

{

s s;

S :: Func(& s);

std :: cout<< s.i_<< ''\\ n';;

}


您可以传递更精细的
而不是仅传递对象带有更多信息的类型:


struct MoreData

{

S * s;

OtherData od;

};


然后在函数中:


static void Func(void * data)

{

MoreData& moreData = * static_cast< MoreData *>(数据);

S& s = moreData.s;

OtherData& otherData = moreData.od;


/ *在这里使用s和其他数据* /

}


Ali



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




" Philip Parker" <博士** @ parker246.freeserve.co.uk>在留言中写道

新闻:cc ********** @ newsg2.svr.pol.co.uk ...

"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:cc**********@newsg2.svr.pol.co.uk...
任何想法?睡眠(1)是一个太长的延迟,我在
中寻找最多50-100毫秒的范围内的东西。
没有太多吸收CPU时间的东西会很好。是否有标准cpp库中的任何内容可以做到这一点?


不,没有。

另一件事。如何创建一个静态类函数成员,在同一个类的对象中访问
的东西?
即我有一个类Bot。在它中将是一个静态函数(需要为
静态进行多线程:/),需要访问特定于
的字符串数组,它被调用的类对象。我怎样才能访问它?
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?




将你想要访问的对象作为参数传递给静态函数。


john



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

john




" Philip Parker" <博士** @ parker246.freeserve.co.uk>在留言中写道

新闻:cc ********** @ newsg2.svr.pol.co.uk ...

"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:cc**********@newsg2.svr.pol.co.uk...
任何想法?睡眠(1)是一个太长的延迟,我在
中寻找最多50-100毫秒的范围内的东西。
没有太多吸收CPU时间的东西会很好。标准cpp库中有什么可以做到的吗?

另一件事。如何创建一个静态类函数成员,在同一个类的对象中访问
的东西?
即我有一个类Bot。在它中将是一个静态函数(需要为
静态进行多线程:/),需要访问特定于
的字符串数组,它被调用的类对象。我怎么才能访问它?
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?




睡眠(50)应该是50毫秒。也许你的意思是微秒?


PKH



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

PKH


这篇关于如何在不使用cpu时间的情况下引入小延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆