rdts 标记时间期限

发布于 2024-07-13 20:25:17 字数 412 浏览 9 评论 0原文

我有一个“听起来”像这样的代码:

线程 1

now = rdtsc();
for_each_member_in_a_list {
   if ( member_in_list.deadline() <= now ) {
      do_something;
   }
}

线程 2

now = rdtsc();
for_each_member_in_a_list {
   member_in_list.updatedealine( foo(now, ...) );
}

现在虽然这在过去运行良好,但现在在 SMP 系统中该代码无法按预期运行。 我想我会使用clock_gettime 和CLOCK_MONOTONIC,但我想先听听其他一些提示。

I have a code that "sounds" like this:

thread 1

now = rdtsc();
for_each_member_in_a_list {
   if ( member_in_list.deadline() <= now ) {
      do_something;
   }
}

thread 2

now = rdtsc();
for_each_member_in_a_list {
   member_in_list.updatedealine( foo(now, ...) );
}

now while this was working good in the past now with a SMP system this code doesn't work as intended. I think I will use clock_gettime with CLOCK_MONOTONIC, but I would like to ear some other hints first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-07-20 20:25:17

理论上,TSC 应该是主板上所有 CPU 之间的同步,但在某些 SMP 系统中却并非如此,这要归咎于主板制造商。 此外,在一些较旧的芯片上,TSC 似乎会随着 CPU 的电源状态而变化,使其可能非常不可靠。 Clock_gettime(CLOCK_MONOTONIC) 更可靠,但开销更大(它是系统调用),但这是迄今为止最好的方法。

我还假设您正在锁定数据结构:)

In theory TSC should be the syncronised between all the CPU's on a motherboard, but in some SMP systems it's not, blame the motherboard manufacturer. Also, on some older chips TSC seems to change with the power state of the CPU making it potentially very unreliable. clock_gettime(CLOCK_MONOTONIC) is more reliable, but has more overhead (it's a systemcall), but is by far the best way to do this.

I also assume you're locking your data structures :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文