我应该如何保护对共享变量的访问?

发布于 2024-10-26 00:14:48 字数 1023 浏览 0 评论 0原文

已经发布了对同一问题的疑问,但我认为答案开始朝另一个方向发展,所以我会尝试集中我的问题:P

1)需要用一些数据填充休向量。为了提高速度,我想使用线程来完成这项工作,因此 1 个线程可以写入向量的前半部分,另一个线程可以写入向量的后半部分。

由于每个线程都访问向量的不同位置...我是否需要保护该访问?

换句话说,我可以在这个结构的两个不同位置同时写入而不对其进行保护吗?

...

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;
}

}

如果我必须保护访问,我应该使用lock_guard还是unique_lock?

2)这个线程解决方案真的会提高速度吗?

我的意思是,即使我保护了这些文字,向量也足够大,无法容纳在缓存中。线程在非常不同的位置上写入,因此“for”将生成如此多的缓存未命中。

这些“缓存未命中”是否会导致执行速度比没有线程时慢?

让1个线程填充偶数,另一个线程填充奇数可以减少缓存未命中吗?

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;
}

谢谢大家:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2024-11-02 00:14:48

1)不,如果保证写入不同的地址,则不需要保护向量。

2)您实际上只需要在自己的机器上亲自测试这些东西即可。尝试单线程、交错访问和分割访问,然后对结果进行计时。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文