I have a very big array read/write by several threads. Each thread will only rw one element of them at a time, so it would be a bad idea to lock the whole array. What I am expecting is something like

// before threads
lock_t Lock[NUM_THREADS]; 

...

// during threads
get_lock(Lock[thread_id], element_id);
array[element_id]+=10; // some operations
release_lock(Lock[thread_id]);

So my question is, what is the best strategy of designing get_lock and release_lock?

有帮助吗?

解决方案

When using OpenMP you can obtain an equivalent behavior using the atomic construct:

// during threads
#pragma omp atomic
array[element_id]+=10; // some operations

Just to give you an idea of its semantic, here is an excerpt from the OpenMP 3.1 standard:

The atomic construct ensures that a specific storage location is accessed atomically, rather than exposing it to the possibility of multiple, simultaneous reading and writing threads that may result in indeterminate values

On the other hand, if you decide to use Intel TBB, you can take a look at the atomic template class.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top