存储一个地址和一个词来形容无锁双链表一个布尔 [英] Store an address and a bool in one word for lock-free doubly linked list

查看:208
本文介绍了存储一个地址和一个词来形容无锁双链表一个布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一些纸无锁双向链表。
在这些文章中,他们一个地址到下一个和preV节点和标志存储在一个字(INT)。

I am reading some paper for lock-free doubly linked list. In these papers, they store an address to next and prev node and a flag in one word(int).

是因为在32位架构的所有地址在4字节边界对齐,因此所有地址是4的倍数?

Is it because in 32-bit architectures all addresses are aligned in 4 byte boundaries so all address are multiple of 4?

如果原因是我说的这是什么code OK?

and if the reason is what i say is this code ok?

const int dMask = 1;
const int pMask = ~dMask;

int store(void* pPointer, bool pDel)
{
    return reinterpret_cast<int>(pPointer) | (int)pDel;
}

void load(int pData, void** pPointer, bool* pDel)
{
    *pPointer = reinterpret_cast<void*>(pData & pMask);
    *pDel = pData & dMask;
}

和另外一个问题:是否在其他平台如Android移动设备,上面的想法是正确的。

And another question: Do in other platforms such as Android mobile devices, above idea is correct?

推荐答案

您是或多或少地正确。这是一个共同的空间优化
在非常低的水平code。它的的便携性。 (你可以做
稍微更便携的使用使用intptr_t 而不是 INT

You're more or less correct. It's a common space optimization in very low level code. It is not portable. (You could make it slightly more portable by using intptr_t instead of int.)

另外,当然,对准仅持有用于指针更
复杂类型;一个的char * 不一定会对齐。 (唯一的
次我看到这个曾经是在记忆的执行
管理,其中,所涉及的所有的块都必须
充分地对准,用于任何类型的)。​​

Also, of course, the alignment only holds for pointers to more complex types; a char* won't necessarily be aligned. (The only times I've seen this used is in the implementation of memory management, where all of the blocks involved are required to be aligned sufficiently for any type.)

最后,我不知道是什么论文的作者正在尝试
这样做,但code您发布不能在多线程中使用
环境,至少在现代机器。以确保
在一个线程的变形被认为是在另一个线程,则需要
使用原子类型,或某种围栏或membar的。

Finally, I'm not sure what the authors' of the paper are trying to do, but the code you post cannot be used in a multithreaded environment, at least on modern machines. To ensure that a modification in one thread is seen in another thread, you need to use atomic types, or some sort of fence or membar.

这篇关于存储一个地址和一个词来形容无锁双链表一个布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆