0

I am having issues when I try to create an application that simulates a restaurant. It needs to have 50 clients, 3 cooks, 1 waiter and 30 tables. When I try to start the threads of the clients, cooks and waiter it crashes and doesnt let me do anything, nor it gives me an error message or warning. I really don't know what to do. I hope you guys can help me with the answer.

Cook class.h: `

#ifndef COOK_H
#define COOK_H

#include <QObject>
#include <QThread>
#include <QSemaphore>

class Cook : public QThread
{
    Q_OBJECT
public:
    Cook(int id, QString name);
    void makeFood(int queueTotal);
    void startCook();
private:
    int id;
    QString name;
    int orderId;
    QSemaphore semaphore;
protected:
    void run(); //makeFood
};

#endif // COOK_H

cook.cpp

#include "cook.h"
#include <QTextStream>


Cook::Cook(int id, QString name)
{
    this->id = id;
    this->name = name;
}

void Cook::startCook(){
    run();
}

void Cook::run(){
   QTextStream(stdout)<< "hello im a cook";
}

`

client.h `

#ifndef CLIENT_H
#define CLIENT_H

#include <QObject>
#include <QThread>
#include <QSemaphore>

class Order;

class Client : public QThread
{
    Q_OBJECT
public:
    Client(int id, QString name);
    void setOrder(Order* order);
    int getId();
    void startClient();
private:
    int id;
    QString name;
    QSemaphore semaphore;
    Order* order;
protected:
    void run();

};

#endif // CLIENT_H

`

client.cpp

`

#include "client.h"
#include <QTextStream>

Client::Client(int id, QString name)
{
    this->id = id;
    this->name = name;
}

void Client::startClient(){
    run();
}

void Client::run(){
    QTextStream(stdout)<< "hello im a client";
}

void Client::setOrder(Order* order){
    this->order = order;
}

int Client::getId(){
    return this->id;
}

`

order.cpp and .h `

#ifndef ORDER_H
#define ORDER_H


class Order
{
public:
    Order(int orderId, bool orderType);
private:
    int orderId;
    bool orderType; //true carne - false vegano
};

#endif // ORDER_H

` order.cpp

`

#include "order.h"

Order::Order(int orderId, bool orderType)
{
    this->orderId = orderId;
    this->orderType = orderType;
}

table.h and cpp

#ifndef TABLE_H
#define TABLE_H

#include <QObject>
class Client;
class Table
{
public:
    Table(int id, QString name, bool seat);
    void clearClient();
    void setClient(Client newClient);
private:
    int id;
    QString name;
    bool seat;
    int clientId;

};

#endif // TABLE_H

table.cpp

#include "table.h"
#include "client.h"

Table::Table(int id, QString name, bool seat)
{
    this->seat = seat;
    this->clientId = 0;
    this->id = id;
    this->name = name;
}

void Table::clearClient(){
    this->seat = false;
    this->clientId = -1;
}

void Table::setClient(Client newClient){
    this->clientId = newClient.getId();
    this->seat = true;
}

`

waiter.h `

#ifndef WAITER_H
#define WAITER_H

#include <QObject>
#include <QThread>
#include <QSemaphore>

class Waiter : public QThread
{
    Q_OBJECT
public:
    Waiter(int id, QString name);
    void startWaiter();
private:
    int id;
    QString name;
    int orderId;
    int tableId;
    QSemaphore semaphore;
protected:
    void run();

};

#endif // WAITER_H

`

waiter.cpp `

#include "waiter.h"
#include <QTextStream>


Waiter::Waiter(int id, QString name)
{
    this->id = id;
    this->name = name;
}

void Waiter::startWaiter(){
    run();
}

void Waiter::run(){
    QTextStream(stdout)<< "hello im a waiter";
}

`

restaurant.h

`

#ifndef RESTAURANT_H
#define RESTAURANT_H


class Restaurant
{
public:
    Restaurant();
    void startRestaurant(int totalChefs, int totalTables, int totalWaiters, int totalClients);
};

#endif // RESTAURANT_H

`

restaurant.cpp

`

#include "restaurant.h"
#include <QThread>
#include "cook.h"
#include "client.h"
#include "waiter.h"
#include "table.h"

Restaurant::Restaurant()
{

}

void Restaurant::startRestaurant(int totalChefs, int totalTables, int totalWaiters, int totalClients){
    QList <Cook*> cookList;
    QList <Waiter*> waiterList;
    QList <Client*> clientList;
    QList <Table*> tables;

    //start
    for(int i=0; i<totalChefs; i++){
        QString output = QStringLiteral("Cook %1").arg(i);
        Cook cook(i, output);
        cookList.append(&cook);
    }

    for(int i=0; i<totalWaiters; i++){
        QString output = QStringLiteral("Waiter %1").arg(i);
        Waiter waiter(i, output);
        waiterList.append(&waiter);
    }

    for(int i=0; i<totalClients; i++){
        QString output = QStringLiteral("Client %1").arg(i);
        Client client(i, output);
        clientList.append(&client);
    }

    for(int i=0; i<totalTables; i++){
        QString output = QStringLiteral("Table %1").arg(i);
        Table table(i, output, false);
        tables.append(&table);
    }


    cookList.value(1)->startCook();
    waiterList.value(1)->startWaiter();
    clientList.value(1)->startClient();
}


`

I am new to QT so please bear that in mind, I hope you guys can help me!

  • 1
    `cookList.append(&cook);` is your bug. The `cook` variable no longer exists after the next `}` which ends the scope and the block for the for () loop. You do the same bug with all other lists. This bug is unrelated to multithreading. – drescherjm Nov 15 '22 at 03:32
  • 1
    Your bug is related to this: [https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – drescherjm Nov 15 '22 at 03:35
  • thanks! I was able to fix it with your help! :) – Yitzhack Ramirez Quiros Nov 16 '22 at 04:04

0 Answers0