Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can multiple threads access a vector at different places?

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

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?

Thanks

like image 716
jmasterx Avatar asked Jun 01 '10 16:06

jmasterx


People also ask

Can multiple threads access same file?

Multiple threads can also read data from the same FITS file simultaneously, as long as the file was opened independently by each thread. This relies on the operating system to correctly deal with reading the same file by multiple processes.

Can multiple threads read the same array?

Would multiple threads each modifying a different array element be writing to the same location in memory? The answer is no. Each array element has a region of memory reserved for it alone within the region attributed the overall array.

What is likely to happen if you have multiple threads accessing the same resource in your program?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

Is vector synchronized in C++?

Below given are the key differences between the C++ Vector and List: As the elements in the Vector are stored in the contiguous memory locations so they are synchronized whereas the elements in the List are stored randomly and connected with each other through the links (pointers) so they are non- synchronized.


1 Answers

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.

like image 154
tzaman Avatar answered Nov 09 '22 04:11

tzaman