What is the fastest way for multiple threads to insert into a vector safely?

I have a program where multiple threads share the same data structure which is basically a 2D array of vectors and sometimes two or more threads might have to insert at the same position ie vector which might result in a crash if no precautions were taken. What is the fastest and most efficient way to implement a safe solution for this issue ? Since this issue does not happen very often (no high contention) I had a 2D array of mutexes where each mutex maps to a vector and then each thread locks then unlocks the mutex after finishing from updating the corresponding vector. If this is a good solution, I would like to know if there is something faster than mutex to use.

Note, I am using OpenMP for the multithreading.


The solution greatly depends on how the problem is. For example:

  • If the vector size may exceed its capacity (ie reallocation is required).
  • Whether the vector is only being read, elements are being inserted or elements can be both inserted and removed.
  • In the first case, you don't have any other possibility than using locks, since you always need to check whether the vector is being reallocated, and wait for the reallocation to complete if necessary.

    On the other hand, if you are completely sure that the vector is only initialized once by a single thread (which is not your case), probably you would not need any synchronization mechanism to perform access to vector elements (inside-element access synchronization may still be required though).

    If elements are being inserted and removed from the back of the vector only (queue style), then using atomic compare and swap would be enough (atomically increase the size of the vector, and insert in position size-1 when the swap was successful.

    If elements may be removed at any point of the vector, its contents may need to be moved to remove empty holes. This case is similar to a reallocation. You can use a customized heap to manage the empty positions in your vector, although this will increase the complexity.

    At the end of the day, probably you will need to either develop your own parallel data structure or rely on a library, such as TBB or Boost.

    链接地址: http://www.djcxy.com/p/51224.html

    上一篇: 为什么Mutex在处置时不会被释放?

    下一篇: 多线程安全地插入矢量的最快方式是什么?