多线程可以在不同的地方访问一个向量? [英] Can multiple threads access a vector at different places?

查看:91
本文介绍了多线程可以在不同的地方访问一个向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个int的向量,我已经预先填充了100个值为0的元素。

Lets say I have a vector of int which I've prefilled with 100 elements with a value of 0.

然后我创建2个线程并告诉第一个线程用数字填充元素0到49,然后告诉线程2用数字填充元素50到99。这可以做吗?

Then I create 2 threads and tell the first thread to fill elements 0 to 49 with numbers, then tell thread 2 to fill elements 50 to 99 with numbers. Can this be done? Otherwise, what's the best way of achieving this?

感谢

推荐答案

是的,这应该很好。只要你能保证不同的线程不会修改相同的内存位置,没有问题。

Yes, this should be fine. As long as you can guarantee that different threads won't modify the same memory location, there's no problem.

这篇关于多线程可以在不同的地方访问一个向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆