问题
First result in Google for "lock free vector" is a research paper cowritten by Damian Dechev, Peter Pirkelbauer and Bjarne Stroustrup describing a theoretical lock-free vector. Has this, or any other lock-free vector, been implemented?
回答1:
MS provides ppl::concurrent_vector and Intel provides tbb::concurrent_vector. On Windows at least ppl and tbb are part of the C-Runtime.
回答2:
I've just looked up what a vector is, in the wikipedia.
I think (only a minute or two of thinking, mind) a fully lock-free version is a bit problematic.
Consider; you create the array, access it as per normal, etc. For this you do not need lock-free. The problem them comes when you need to resize. The thread which comes in and discovers this needs to malloc. This is a truly unique operation - other threads at this point must block/spin. If they tried to help, e.g. do the malloc themselves, you could have many threads issuing the malloc. Now it might be in practise the number of threads doing is this is so low it's okay - in which case you may have a number of threads performing the malloc, with the winner atomically activating the new memory and the losers seeing this and then free()ing their memory.
Then to perform the copy, when a thread comes to access an element, well, we'll need to keep track of all of the allocated arrays in a list, and we access them through the list, oldest first, till we find the element we want and if it's not in the most recent array, we move it and then access it.
We then also need a way for a thread to know it has moved the final element and can free that array, so we'll have to count elements; so we have a risk of potentially unbounded allocation requirements. What's more, the data structure (I've said a list, but it could be other things, although they won't be as good, prolly) will need to be atomic (since maybe threads could delete an array at the same time) and an atomic lock-free list was until very recently the pinnacle of lock-free data structures, as complex as they get, requiring SMR and with a complex implementation.
(I say until recently - someone recently invented a lock-free red-black tree, the whole thing, add and delete!)
TBH, I don't understand why anyone would use a vector. Why not just use a balanced binary tree or a hash?
来源:https://stackoverflow.com/questions/9385969/is-there-a-lock-free-vector-implementation