Hi, I am working on a program to simulate a zoo, essentially there are passengers that are waiting for a ride in cars around the zoo. This is a multi-threaded program using the std::thread class, and essentially I pass an array of passengers and an array of cars into a function as a thread is initialized, then based on some the values of the objects, I place cars and passengers each into a std::queue. Then each passenger is paired with a car until one of the queues is empty. There is much more to the program than this, but I think this is enough to show the problem I am having.
The problem is that when I modify the member variables of the "front" member of each queue, the values don't stick when I access the values directly. You can see in the code below that I try to print the value
Here is the header of the function that the next code snippets are from.
void carsManage(int time, car cars[], gasStation& station, visitor visitors[], int numVisitors, int numPumps, int numCars, int rideTime)
here you can see the queues are initialized and filled with certain objects
1 2
|
queue<visitor> rideQueue;
queue<car> carQueue;
|
1 2 3 4 5 6 7 8 9 10 11
|
for (int i = 0; i < numVisitors; i++)//checking for visitors that need rides
{
if (!visitors[i].getRidden())
rideQueue.push(visitors[i]);
}
for (int i = 0; i < numCars; i++)//adding all ready cars to queue
{
if (cars[i].getReady())
carQueue.push(cars[i]);
}
|
In the code below there are two std::cout statements that should return the same values for each object, but they do not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
while (rideQueue.size() > 0 && carQueue.size() > 0)//putting drivers in cars until one runs out
{
rideQueue.front().setRidden(true);
rideQueue.pop();
carQueue.front().setRides(carQueue.front().getRides() - 1);
carQueue.front().setReady(false);
carQueue.front().setLeaveTime(time);
cout << carQueue.front().getLeaveTime() << "\n";
carQueue.pop();
}
int carsOut = 0;
for (int i = 0; i < numCars; i++)//calculating cars out driving
{
cout << cars[i].getLeaveTime() << "\n";
if (cars[i].getLeaveTime() <= time && cars[i].getLeaveTime() > 0)
carsOut += 1;
}
|
also, here is the thread initialization, if that helps clarify anything
thread carAgent(carsManage, time, cars, ref(station), visitors, numVisitors, numPumps, numCars, rideTime);
If anything needs to be clarified, let me know; Any help would be greatly appreciated.