开发者

How should i protect the access to a shared variable?

开发者 https://www.devze.com 2023-02-19 09:46 出处:网络
Already posted a doubt about the same issue, but i think the answers started to go in other direction, so i will try to focus my questions :P

Already posted a doubt about the same issue, but i think the answers started to go in other direction, so i will try to focus my questions :P

1) Need to fill a hughe vector with some data. To improve the speed i want to use threads to do the job, so 1 thread can write the first half of the vector and the other thread write the second half.

Since each thread is accesing different positions of the vector... Do i need to protect that acces?

In other words, can i write at the same time in 2 different positions of this structure without protecting it?

...

using namespace std;

...

main{

int n = 256x1024x1024;
vector<int> vec(n);

thread t1(fillFunction(std::ref(vector), 0, n/2);
thread t2(fillFunction(std::ref(vector), n/2, n);

t1.join;
t2.join;

}

fillFunction(vector<int> &vec, int first, int final){
int i;
for (i = first; i < final; i++){
vec[i] = some_data;
}

}

In case i have to protect the开发者_如何学运维 access, should i use lock_guard or unique_lock?

2) This thread solution is really going to improve the speed?

I mean, even if i protect the writings, the vector is large enough to not fit on cache. The threads are writing on very different positions, so the 'for' will generate so many cache misses.

Can these "cache misses" result in a slower execution than without threads?

Making 1 thread to fill the even numbers and the other thread the odd numbers can reduce the cache misses?

thread t1(fillFunction(std::ref(vector), 0, n/2);
thread t2(fillFunction(std::ref(vector), 1, n);

[...]

for (i = first; i < final; i = i+2){
vec[i] = some_data;
}

Thank you all :)


1) No you do not need to protect the vector if you are guaranteed to write to different addresses.

2) You will really just have to test these things yourself on your exact machine. Try single thread vs. interleaved access vs. split access and just time the results.

0

精彩评论

暂无评论...
验证码 换一张
取 消