对未对齐数据的互锁操作

发布于 2024-07-23 01:28:24 字数 118 浏览 5 评论 0原文

win32 互锁函数提供了对数据进行原子操作的机制。 它们应该是线程安全和多处理器安全的。

如果数据未对齐会怎样? 联锁操作仍然是原子的? 例如:递增未对齐的整数。

The win32 interlocked functions provide a mecanism for atomic operation on data. They are supposed to be thread-safe and multiprocessor-safe.

What happen if the data is not aligned? the interlocked operations are still atomic?
Ex.: incrementing a integer that is not aligned.

Ty

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

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

发布评论

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

评论(1

谷夏 2024-07-30 01:28:24

如果您阅读了大多数 Interlocked API 函数,就会发现一些注释,其中指定了以下内容:

“Addend 参数指向的变量必须在 32 位边界上对齐;否则,该函数在多处理器 x86 上的行为将无法预测系统和任何非 x86 系统。”

有关示例,请参阅此 MSDN 文档

这基本上表明您需要使用 _aligned_malloc 对齐数据以获得正确的结果。

If you read most of the Interlocked API functions, there are remarks that specify something along the lines of:

"The variable pointed to by the Addend parameter must be aligned on a 32-bit boundary; otherwise, this function will behave unpredictably on multiprocessor x86 systems and any non-x86 systems. See _aligned_malloc."

For an example, see this MSDN doc.

This basically says you need to align your data for proper results, using _aligned_malloc.

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