I am new to C++ and I am trying to get a threads to randomly move on a grid in any direction until its final destination is reached. I am not sure how to write that in the while loop of the walkerI function. I looked at many tutorials and read through c++ syntax but I am unsure on how to go about this. Maybe I am missing something very basic. Can I get some explanation on this/useful websites on how to do this. Thank you!
#include <iostream>
#include <mutex>
#include <thread>
#define N 1000
#define S 25
#define MAX_WALKERS_PER_LOCATION 3
#define MAX_WALKERS_PER_EDGE 4
std::thread thr[N];
std::mutex m;
typedef struct Walker
{
int currentX, currentY, finalX, finalY;
bool hasArrived;
void Init()
{
currentX = rand() % S;
currentY = rand() % S;
finalX = rand() % S;
finalY = rand() % S;
hasArrived = false;
}
} ;
int originalGridCount[S][S]; //used to make sure the number of walkers per location is within the limits.
int finalGridCount[S][S]; //used to set the result target.
int obtainedGridCount[S][S]; //something you may use for the results
Walker walkers[N];
void Lock(std::mutex* m)
{
m->lock();
std::this_thread::sleep_for(std::chrono::milliseconds(1)); //Sleeps to simulate longer execution time and increase the probability of an issue
}
void Unlock(std::mutex* m)
{
m->unlock();
}
void PrintGrid(std::string message, int grid[S][S])
{
std::cout << message;
for (int i = 0; i < S; i++)
{
for (int j = 0; j < S; j++)
std::cout << (grid[i][j]<10?" " : " ") << grid[i][j]; //Initialising grids
std::cout << "\n";
}
}
void SetObtainedGrid()
{
for (int i = 0; i < N; i++) //Initialising walkers' locations
{
obtainedGridCount[walkers[i].currentY][walkers[i].currentX]++;
if (!walkers[i].hasArrived)
{
std::cout << "\nAt least one walker had not arrived!\n";
return;
}
}
}
void CompareGrids(int a[S][S], int b[S][S])
{
for (int i = 0; i < S; i++)
for (int j = 0; j < S; j++)
if (a[i][j] != b[i][j])
{
std::cout<<"\nError: results are different!\n";
return;
}
std::cout << "\nSeems to be OK!\n";
}
void WalkerI(int id)
{
while(finalGridCount != originalGridCount){
}
}
void InitGame()
{
for (int i = 0; i < S; i++)
for (int j = 0; j < S; j++)
originalGridCount[i][j] = finalGridCount[i][j] = obtainedGridCount[i][j] = 0; //Initialising grids
for (int i = 0; i < N; i++) //Initialising walkers' locations
{
do walkers[i].Init();
while (originalGridCount[walkers[i].currentY][walkers[i].currentX]>= MAX_WALKERS_PER_LOCATION);
originalGridCount[walkers[i].currentY][walkers[i].currentX]++;
}
for (int i = 0; i < N; i++) //Initialising walkers' locations
finalGridCount[walkers[i].finalY][walkers[i].finalX]++;
}
int main()
{
InitGame();
//Start your threads here.
for (int i = 0; i < N; i++)
thr[i] = std::thread(WalkerI, i);
//Wait for completion of all threads
for (int i=0; i < N; i++)
thr[i].join();
PrintGrid("Original locations:\n\n", originalGridCount);
PrintGrid("\n\nIntended Result:\n\n", finalGridCount);
SetObtainedGrid();
PrintGrid("\n\nObtained Result:\n\n", obtainedGridCount);
CompareGrids(finalGridCount, obtainedGridCount);
}
Aucun commentaire:
Enregistrer un commentaire