Skip to content

Commit

Permalink
Extend the test and add explanation comment
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksei-fedotov committed Oct 24, 2024
1 parent da909fe commit d9e4ce0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 6 additions & 2 deletions include/oneapi/tbb/detail/_segment_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,14 @@ class segment_table {
}

void extend_table_if_necessary(segment_table_type& table, size_type start_index, size_type end_index) {
// extend_segment_table if an active table is an embedded table
// and the requested index is not in the embedded table
// Extend segment table if an active table is an embedded one and the requested index is
// outside it
if (table == my_embedded_table && end_index > embedded_table_size) {
if (start_index <= embedded_table_size) {
// More than one thread can get here: the one that has assigned the first block and
// is in the process of allocating it now, and the one that saw the first block has
// been assigned already, but not yet allocated. This latter thread decides not to
// wait for the first one and extend the table itself.
try_call([&] {
segment_table_type new_table =
self()->allocate_long_table(my_embedded_table, start_index);
Expand Down
18 changes: 14 additions & 4 deletions test/tbb/test_concurrent_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,20 +699,30 @@ TEST_CASE("Testing vector in a highly concurrent environment") {
std::vector<int> grow_by_vals(num_inserts);

for (int i = 0; i < num_repeats; ++i) {
int expected_size = 0;
int expected_size = 0, expected_sum = 0;
std::generate(grow_by_vals.begin(), grow_by_vals.end(),
[&gen, &uniform_dist, &expected_size]() {
[&gen, &uniform_dist, &expected_size, &expected_sum]() {
const int random_value = uniform_dist(gen);
expected_size += random_value;
expected_sum += random_value * random_value;
return random_value;
});

tbb::concurrent_vector<double> test_vec;
tbb::concurrent_vector<int> test_vec;
tbb::parallel_for(0, num_inserts, [&] (int j) {
test_vec.grow_by(grow_by_vals[j]);
tbb::concurrent_vector<int>::iterator start_it = test_vec.grow_by(grow_by_vals[j]);
tbb::concurrent_vector<int>::iterator end_it = start_it + grow_by_vals[j];
do {
*start_it = grow_by_vals[j];
} while (++start_it != end_it);
});

REQUIRE(test_vec.size() == expected_size);
int actual_sum = 0;
for (int j = 0; j < expected_size; ++j) {
actual_sum += test_vec[j];
}
REQUIRE(expected_sum == actual_sum);
}
}

Expand Down

0 comments on commit d9e4ce0

Please sign in to comment.