Are the vectors defined in the C++ STL re-entrant or thread safe?
Can I use two threads and work(in this case sort) on two halfs of a vector without using a mutex?
For example:
int size = (Data_List.size())/2;
Thread A()
{
................ // Do we need to lock Data_list with a mutex
sort(Data_List.begin(),Data_List.begin()+size,cmp);
}
Thread B()
{
....// Do we need to lock Data_list with a mutex
sort(Data_List.begin()+(size+1),Data_List.end(),cmp);
}
My Question is do we need to lock the access of Data_List using a mutex?
Note: the cmp function is a regular int comparision function.
As long as the threads are working on different regions of memory and your comparison function only works with that memory and local variables, you should be ok. Essentially, you are “locking” each half of the table by dividing the work between the threads and only letting the thread work on its half of the data.