Is there a way to loop through all elements of a vector in parallel in C++?
NickName:Quentin_P Ask DateTime:2019-04-13T19:46:06

Is there a way to loop through all elements of a vector in parallel in C++?

I am working on an elevator simulator in c++. I have a class passenger and the program creates "randomized" instances of that class with different starting floors and different destinations within the building then stores them in a vector

vector<passenger> passengers;

I am working on the dispatch algorithm for the 4 elevators in the building. However, because of the fact that the algorithm compares all 4 elevators in the building, it is possible that the passenger cannot have an elevator assigned right away (i.e. not available IDLE elevator or elevator having enough space going in the same direction as the passenger).

Problem:

I want to apply the algorithm constantly to all the elements of the vector passengers simultaneously and can't do it with a traditional for loop as it could get stuck mid-way with passenger instances down the vector that could be assigned an elevator left waiting.

Anyone knows if there is a way to do so?

Thanks.

Copyright Notice:Content Author:「Quentin_P」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55665087/is-there-a-way-to-loop-through-all-elements-of-a-vector-in-parallel-in-c

More about “Is there a way to loop through all elements of a vector in parallel in C++?” related questions

Is there a way to loop through all elements of a vector in parallel in C++?

I am working on an elevator simulator in c++. I have a class passenger and the program creates "randomized" instances of that class with different starting floors and different destinations within ...

Show Detail

Operating on different elements of std::vector in parallel

Say I have a std::vector&lt;Object*&gt;. The vector is large ( > 1000 elements) and each Object* needs to have extensive computation done on it. The for loop that runs each the computation on each

Show Detail

C++: Allocate memory for an std::vector then initialize its elements in parallel

I have a use case for creating an std::vector with many elements, each of which is of a simple, but non-primitive, type (POD struct). The vector and type are sufficiently large/complex that in the

Show Detail

C++: how to loop through integer elements in a vector

I would like to loop through elements of a vector in C++. I am very new at this so I don't understand the details very well. For example: for (elements in vector) { if () { check som...

Show Detail

best way to sum up Matrix elements parallel in c#

What is the fastest most efficient way to sumUp all matrix elements parallel in .net 4.0 ? using Parallel.For end in inner loop making lock(objectLock) { result += matrix[i, j] } is 2 times slowwe...

Show Detail

parallel_for writing to an unsorted vector

I have a parallel_for loop that iterates through a large vector, analyses different portions of it (all possible sequential sequences, 1-2, 1-3, 1-4, 2-3, 2-4, etc.) and stores statistic information

Show Detail

Compare all elements in a vector to all elements of a list without for loop

This is what i am trying to do List &lt;- list(LETTERS,LETTERS,LETTERS) Vector &lt;- c("A","B","C","D") I want to know whether each element of the vector is present in each element of l

Show Detail

For loop not iterating through all values in vector

I am trying to sum the NA values per year of multiple time series. I have created a vector of just my dates that I am then comparing to a vector of both dates and ozone values. I have first created...

Show Detail

check if ALL elements of a vector are in another vector

I need to loop through coloumn 1 of a matrix and return (i) when I have come across ALL of the elements of another vector which i can predefine. check_vector = [1:43] %% I dont actually need to

Show Detail

Loop through a vector and copy the elements into a C array

The project I am working on requires a bunch of translation from C++ concepts to C. In this use case, the caller is responsible for managing the memory buffer. He then calls the function and prov...

Show Detail