我有一个小数纪元时间戳,表示为double,我想转换为适当的std::chrono::time_point.这个时代是自1970年1月1日以来的常见UNIX时代.我知道存在std::chrono::system_clock::from_time_t,但是time_t没有一个小部分.使用C++ 11的最佳方法是什么?
这个问题与boost :: posix_time :: ptime的unix时间戳有关,除了它要求的是C++ 11而不是Boost版本.
boost::this_thread::sleep()似乎使用的对象的例子boost::posix_time::milliseconds.我已经尝试了它并且它可以工作,但我boost::chrono用于检查系统时钟等.在我看来,我应该能够通过sleep()一个chrono::duration这样的:
boost :: this_thread :: sleep(boost :: chrono :: duration(10));
但是编译器给出了以下错误:
... boost_1_49_0\boost/thread/win32/thread_data.hpp(171):错误C2039:'total_milliseconds':不是'boost :: chrono :: duration'的成员
我发现这令人困惑.我是否正确地认为我应该能够做到这一点?是否有必要转换为posix_time?
我对新的C++ chrono库肯定有点失落.
这里我有一个更新循环.它运行两个操作:
engine.Update()
engine.Render()
Run Code Online (Sandbox Code Playgroud)
这些都是长期操作,很难说它们有多长.
因此,我们测量他们花了多长时间,然后进行一些计算并找出在调用渲染之前逐渐调用更新的最佳方法.
为此,我正在使用C++ 11的Chrono功能.我选择它是因为它听起来很划算:更准确,更依赖于平台.我发现现在我遇到的问题比现在更多.
以下是我的代码,以及我的主要问题.非常需要任何有关问题或正确操作方法的帮助!
我直接在相关行旁边的注释中标记了我的问题,我将在下面重复这些问题.
头文件:
class MyClass
{
private:
typedef std::chrono::high_resolution_clock Clock;
Clock::time_point mLastEndTime;
milliseconds mDeltaTime;
}
Run Code Online (Sandbox Code Playgroud)
简化的更新循环
// time it took last loop
milliseconds frameTime;
// The highest we'll let that time go. 60 fps = 1/60, and in milliseconds, * 1000
const milliseconds kMaxDeltatime((int)((1.0f / 60.0f) * 1000.0f)); // It's hard to tell, but this seems to come out to some tiny number, not what I expected!
while (true)
{
// …Run Code Online (Sandbox Code Playgroud) 以下代码执行compile(g++ 4.7.2):
#include <chrono>
typedef std::chrono::duration< double > double_prec_seconds;
typedef std::chrono::time_point< std::chrono::system_clock > timepoint_t;
void do_something( const timepoint_t& tm )
{
// ...
}
int main( int argc, char** argv )
{
timepoint_t t0 = std::chrono::system_clock::now();
timepoint_t t1 = t0 + std::chrono::seconds(3);
// timepoint_t t3 = t0 + double_prec_seconds(3.14);
auto t3 = t0 + double_prec_seconds(3.14);
do_something( t1 );
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道什么类型t3.它不是timepoint_t,并且用明确给出的类型取消注释行将无法编译.与函数调用相同的:我不能叫do_something用t3.
所以我的问题是:
我知道我可以使用这样的额外演员
// this works
timepoint_t t3 = …Run Code Online (Sandbox Code Playgroud) 我一直在研究各种游戏计时循环方法,例如Glenn Fiedler和DeWitter.由于我自己的C++知识限制,我发现关键领域难以理解.有了这个我开始尝试实现我自己的方法....我想一个好方法来尝试理解这些方法的一些东西.
[edit1:我正在使用CodeBlocks IDE和minGW-w64(x64-4.8.1-posix-seh-rev5)作为编译器]
[edit2:修改代码和输出窗口以包含第3个计时器,QueryPerformanceCounter]
在尝试完成此操作时,我遇到了以下问题:
最小代码:
#include <chrono>
#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;
using namespace chrono;
LARGE_INTEGER startqpc, stopqpc, li;
double PCFreq = 0.0;
void print()
{
for (int p=0; p<1000000; ) p += 1; //adjust till ms (steady) returns 1-2ms
}
int main()
{
for(int x=0; x<200; x += 1)
{
steady_clock::time_point start = steady_clock::now();
auto timePoint1 = chrono::high_resolution_clock::now();
if(!QueryPerformanceFrequency(&li))
cout << "QueryPerformanceFrequency failed!\n";
PCFreq = double(li.QuadPart)/1000.0;
QueryPerformanceCounter(&startqpc);
print();
steady_clock::time_point finish = steady_clock::now();
auto …Run Code Online (Sandbox Code Playgroud) 我在阅读文章时遇到了一个代码,作者声称"C++标准库提供了以下类型定义:"
namespace std {
namespace chrono {
typedef duration<signed int-type >= 64 bits,nano> nanoseconds;
typedef duration<signed int-type >= 55 bits,micro> microseconds;
typedef duration<signed int-type >= 45 bits,milli> milliseconds;
typedef duration<signed int-type >= 35 bits> seconds;
typedef duration<signed int-type >= 29 bits,ratio<60>> minutes;
typedef duration<signed int-type >= 23 bits,ratio<3600>> hours;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是什么signed int-type >= 64 bits意思?这是否意味着signed int减去type?如果是这样,你怎么解释?
我正在尝试一个简单的程序来打印时间戳值,steady_clock如下所示:
#include <iostream>
#include <chrono>
using namespace std;
int main ()
{
cout << "Hello World! ";
uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
cout<<"Value: " << now << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但每当我这样编译时g++ -o abc abc.cpp,我总是会收到一个错误:
In file included from /usr/include/c++/4.6/chrono:35:0,
from abc.cpp:2:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
abc.cpp: In …Run Code Online (Sandbox Code Playgroud) 我想创建一个类,其构造接受一个std::chrono::duration参数并将结果存储在一个成员中,以便我以后可以将其传递给std::this_thread::sleep_for().
我知道我可以编写一些函数模板,其工作sleep_for方式如下:
template <typename Rep, typename Period>
void mySleep( std::chrono::duration<Rep, Period> time )
{
std::this_thread::sleep_for(time);
}
Run Code Online (Sandbox Code Playgroud)
这可能是一个类的成员函数.但是下面这个案子呢?
class UsesDuration
{
public:
template <typename Rep, typename Period>
UsesDuration( std::chrono::duration<Rep, Period> dur ) :
my_duration(dur) { }
void doSomethingPeriodic()
{
while( some_condition )
{
std::this_thread::sleep_for(my_duration);
somethingInteresting();
}
}
private:
??? my_duration; /* How do I declare this??? */
}
Run Code Online (Sandbox Code Playgroud)
是否有一种干净的方法来保持持续时间"抽象"A)理想情况下不将整个班级变成模板化的类,B)将类转换为类模板?
C++ 11标准讨论了如果调整系统时钟以使传递到的时间点sleep_until()现在已经过去会发生什么 - 但是当指定的时间点已经存在时,我无法看到解决该问题的任何地方.过去.
我是否只是忽略了某些东西,或者它是否真的没有指定 - 即使是UB或实现定义的东西?
如果sleep_for()以负持续时间调用,则会出现类似的问题.
考虑一个定制类型,该类型用于乘以和除以持续时间的特定实例:
#include <chrono>
#include <iostream>
class Foo {};
using Duration = std::chrono::seconds;
inline Duration operator*(Duration d, Foo) {
std::cout << "multiplying some time with Foo\n";
return d;
}
inline Duration operator/(Duration d, Foo) {
std::cout << "dividing some time by Foo\n";
return d;
}
int main() {
Duration d;
Foo f;
d * f;
d / f;
}
Run Code Online (Sandbox Code Playgroud)
此代码使用gcc编译时不会发出警告,但使用clang(wandbox)时失败
In file included from prog.cc:1:
/opt/wandbox/clang-7.0.0/include/c++/v1/chrono:1259:81: error: no type named 'type' in 'std::__1::common_type<long long, Foo>'
typename common_type<typename _Duration::rep, _Rep2>::type>::value> …Run Code Online (Sandbox Code Playgroud)