- 论坛徽章:
- 0
|
以为没有人回复呢~
我开了50个线程在 centos上循环跑,偶尔会出现得到的tv_sec 大4500多秒。
于是我使用 time(0)来代替,也会出现这样的情况
struct timeval now;
int ret = gettimeofday(&now, NULL);
time_t s = ::time(0);
assert(ret == 0);
assert (now.tv_sec <= s );
assert 失败,结果会大 4500s。
google 关键词“gettimeofday线程安全”,有一篇
《mktime,localtime_r,gettimeofday是线程安全的吗?》
http://hi.baidu.com/pkuyikai/blo ... bfd23e0cd7da86.html
google “gettimeofday thread-safe”,这篇比较靠前,也有人引用
http://osdir.com/ml/lib.common-c++.bugs/2004-05/msg00007.html
“The problem, according to POSIX documentation, is that time(), and
> therefore gettimeofday(), keeps some static data that is overwritten by
> each call to the function”
“ |
|