concurrent access and free of a data structure

懵懂的女人 提交于 2019-12-23 20:53:23

问题


The problem is like this:

I have an array of 500 pointers which point to 500 elements in a doubly linked list. There are 10 threads which run in parallel. Each thread runs 50 loops, and tries to free some element in the list.

The list is sorted (contain simple integers), and there are 10 other threads running in parallel, searching for the node that contains a particular integer and access the other satellite data in this node. So the node is like:

struct node
{
   int key;         // Key used to search this nodes
   int x,y,z;       // Satellite data
   struct node *prev;
   struct node *right;
};

The problem is easily solvable if I just lock the list before search / delete. But that is too coarse grained. How do I synchronize these threads so that I can achieve better concurrency?

Edits:

  1. This is not a homework question. I do not belong to academia.
  2. The array holding 500 pointers seems weird. I have made it like that to visualize my problems with least possible complexity.

回答1:


I can think of a couple of broad approaches which don't involve a global lock, and should allow some degree of forward progress:

1. mark but don't remove

When a deletion thread identifies its victim, mark it as deleted but leave it in place. When a search thread encounters a node with this deleted mark, it just ignores it.

You'll need to issue a write/release barrier after marking the node deleted, and an acquire barrier before inspecting the value: you'll need platform-specific, compiler-specific extensions, otherwise you're writing those barriers in assembler.

2. genuine removal with a lockfree list

As per the paper in Peeyush's answer; similar platform- or compiler-specific requirements for CAS, and significant care is required. Options such as refcounts or hazard pointers can allow the node to be genuinely deleted once no-one is looking at it. You may find you need to replace your prev/next pointers by short indices you can pack into a single word for CAS to work: this means bounding the number of nodes and allocating them in an array.

Also note that although every thread should be able to make progress with this sort of scheme, individual operations (eg. traversing to the next node) may become much more expensive due to the synchronisation requirements.




回答2:


You might consider lock-free linked list using CompareAndSwap operation.

link to paper




回答3:


You need to lock any data that can change. If you will do a lot of work, create one lock per item in the list. A thread has to have the previous, the current, and the next item locked in order to remove the middle one. Make sure to always get locks in the same order to avoid deadlocks.

Other delete threads and the search threads will have to wait until the object is removed and the new links set up. Then the locks are released and they can continue.



来源:https://stackoverflow.com/questions/9115876/concurrent-access-and-free-of-a-data-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!