Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between TBB Concurrent Vector and STL Vector?

Tags:

c++

stl

tbb

TBB Concurrent Vector we can dynamically resize by using grow_by and grow_to_at_least .And in STL Vector there is also resize function .So what is the difference?

The difference which i came across is

1. A concurrent_vector never moves an element until the array is cleared, which can be an advantage over the STL std::vector(which can move elements to resize the vector), even for single-threaded code.

2. Use concurrent_vector only if you really need to dynamically resize it while other accesses are (or might be) in flight, or if you require that an element never move.

Can anyone please explain these points as i am confused in this?

like image 829
Jasdeep Singh Arora Avatar asked Aug 27 '26 07:08

Jasdeep Singh Arora


1 Answers

I get this to mean that once memory is allocated in concurrent_vector it is always used, as opposed to std::vector which allocates twice as much memory when it runs out and moves the objects stored to the newly allocated block.

concurrent_vector, I assume, is adding new blocks of memory but keeps using the old ones. Not moving objects is important as it allows other threads to keep accessing the vector even as it is being re-sized. It probably also helps with other optimizations (such as keeping cached copies valid.) The downside is access to the elements is slightly slower as the correct block needs to be found first (one extra deference.)

Here's an explanation of std::vector memory allocation: How is dynamic memory managed in std::vector?

like image 162
Eli Algranti Avatar answered Aug 28 '26 21:08

Eli Algranti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!