简体   繁体   English

列表上的原子操作

[英]Atomic operations over a list

Suppose I have a list, and I want to use a test_and_set operation a parameter of which is the calculation of some pointer address l->a.next->next . 假设我有一个列表,我想使用test_and_set操作,其参数是一些指针地址l->a.next->next This, I think, will not be atomic, and the test_and_set will be useless. 我认为这不是原子的, test_and_set也没用。 Is there a way to calculate that pointer value atomically so that the TAS will work atomically? 有没有办法以原子方式计算指针值,以便TAS原子地工作?

You probably mean CAS (more useful). 你可能意味着CAS (更有用)。

In general: yes it is often used to implement transactional or wait-free datastructures, 通常:是的,它通常用于实现事务性或等待的数据结构,

Buf first things first: let's separate address-calculations from atomic operations on an address, you first get the specific address at which something should be swapped, the CAS does not care how you got there. 首先要做的事情是:让地址计算地址上的原子操作分开,首先得到应该交换某些东西的具体地址,CAS不关心你是如何到达那里的。

Specifically you should first let each of your threads navigate the list until they find a place where they want to replace a next-pointer, then they can try-repeat this by using CAS. 具体来说,您应首先让每个线程导航列表,直到找到他们想要替换下一个指针的位置,然后他们可以尝试使用CAS重复此操作。 It depends on your scenario whether the thread has to re-walk the list for the retry. 这取决于您的场景线程是否必须重新遍历列表以进行重试。

The tricky part is actually at a different place in the code: where you free (or re-use) the list-nodes. 棘手的部分实际上是在代码中的不同位置:您释放 (或重用)列表节点。 In general you have to assume that you cannot re-use or free node-chains that were disconnected from your list ever . 一般来说,你必须承担,你不能重复使用或从列表中断开以往任何时候都免费节点链。 In practice there are heursitics you can use but this depends on your use-case. 在实践中,你可以使用heursitics,但这取决于你的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM