mercredi 17 novembre 2021

(C++) Threads and concurrent programming for a grid walk program [closed]

I am trying to make a grid program with N walkers that can move in any one direction at a time (north,east,south,west). They will have a predefined starting point currentX and currentY and a final predefined point to reach finalX and finalY. The grid is set to 25x25. The aim of the program is to check that all threads have reached its final location by comparing the finalGridCount with obtainedGridCount.

My question is should I create a new function for the random movement of walkers or create it in the walkerI function which is called by threads in main. Also could I get a guidline on how to do the movement.

Code below to give a better understanding:

#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 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 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