如何使用timeval表示10毫秒?

7 c unix

如何使用timeval表示10毫秒?

这是我到目前为止:

struct timeval now;
now.tv_usec =10000; 
Run Code Online (Sandbox Code Playgroud)

Ada*_*eld 27

A struct timeval表示时间,以秒数(tv_sec)加上tv_usec0到999,999之间的微秒数().因此,要表示10毫秒,您将使用10,000微秒,如您所建议:

struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;
Run Code Online (Sandbox Code Playgroud)

  • 只是一个小点.根据http://www.gnu.org/s/hello/manual/libc/Elapsed-Time.html,tv_usec值始终小于一百万,即介于0和999,999之间.否则你可以在tv_sec和tv_usec字段中代表第二个,这根据他们的定义没有意义. (3认同)

Smi*_*ver 13

有关将毫秒转换为timeval struct的更一般情况,请使用:

int milliseconds = 10;
struct timeval now;
now.tv_sec = milliseconds / 1000;
now.tv_usec = (milliseconds % 1000) * 1000;
Run Code Online (Sandbox Code Playgroud)


Sil*_*rom 5

它的

 struct timeval {
    int tv_sec;    // seconds 
    int tv_usec;   // microseconds!
Run Code Online (Sandbox Code Playgroud)

所以现在.

tv_sec = 0;
tv_usec = 10000; 
Run Code Online (Sandbox Code Playgroud)

`是对的