1

I am working on a final project for a class. This project is to mimic multiple atm's. That is my program already runs. Inside of my main.cpp, I created the threads, for now just two, later on maybe more, They call a class Begin that rand() if customers are going to make a deposit or withdraw and then rand() the amount they are going to use and does this 5 times.

#include "ATM.h"

void main()
{   

  Begin test1;

  test1.manager();

  thread first(&Begin::atm, test1);

  thread second(&Begin::atm, test1);

  first.join();
  second.join();

  delete resbox::cashbox;

  system("pause");
}

I cannot figure out how to suspend my threads created in Main.cpp inside of my observe() function like so:

    void watcher::observe()
{
    float cash;
    if (resbox::cashbox->gettotal() >= resbox::cashbox->getmax())
    {
        //suspend all other threads

        cout << "Please empty cash box it is full! with $"<< resbox::cashbox->gettotal() << endl;
        cout << "How much would like to withdraw?" << endl;
        cin >> cash;
        resbox::cashbox->cashwd(cash);
        cout << "This is the amount in the reserve box now is $" << resbox::cashbox->gettotal() << endl;

        //resume all other threads
    }
    if (resbox::cashbox->gettotal() <= 500)
    {
        //suspend all other threads
        cout << "Please fill cashbox it is low, has $" << resbox::cashbox->gettotal() << endl;
        cout << "How much would like to add?" << endl;
        cin >> cash;
        resbox::cashbox->cashdp(cash);
        cout << "This is the amount in the reserve box now $" << resbox::cashbox->gettotal() << endl;

        //resume all other threads

    }
}

Whenever the condition is met for one of the if statements I need to be able to suspend all other threads except the current thread that met the condition. Then after the data is completed before leaving the if statement and observer functions resume all other threads.

I read about the possibility of using SuspendThread, and ResumeThread from here, how to suspend thread. Yet I am having a hard time passing the threads created in main.cpp to the observer function so that I could call those functions. I figured out how to create threads from cplusplus.com, I also notice I could potentially use a mutex locking as refered to from What is the best solution to pause and resume pthreads?

I am using c++ under Microsoft Visual Studio 2015 Community.

This is my first time dealing with threads. For my use which is better, pass the created threads to the observer function, or is there another to pause/suspend and then resume them and how would i do so? Thank you for any advice/help provided.

Currently If I run my program and one of the conditions is met by a thread, the other thread will also meet the same condition and I have to enter the amount to deposit/withdraw twice before the threads continue until each thread has dealt with 5 customers each for a total of 10 customers.

Community
  • 1
  • 1
  • Apparently I need to create a shared mutex and then have a reader lock and unlock inside atm function, and have a writer lock unlock inside the oberver function not entirely sure how to work with mutex any help appriciated. thanks – Joseph Ryan Apr 18 '17 at 02:42

1 Answers1

0

I finally figured out what I needed and what to use thanks to: Class RWLock

By utilizing this class, inside my project. Then creating a global instance of that class.

Then I added the reader and writer lock and unlocks where it function inside my code the best. Like so:

  void Begin::atm() //The main function that makes it easier for threads to 
  call and run the Program.
{
    ATM atm;
    int choice, amount;
    LARGE_INTEGER cicles;
    QueryPerformanceCounter(&cicles);
    srand(cicles.QuadPart);

    for (int i = 0; i < imax; i++) //mimics a total of 5 customers
    {
        rw.ReadLock(); //Have to place to read lock here.
        choice = rand() % 2; //Randomizes the choice of depositing or withdrawing.
        amount = rand() % 5000 + 1; //Randomizes 'the amount of cash that the customers use.
        rw.ReadUnlock(); //Read unlock must happen here otherwise it blocks the writers.

        rw.WriteLock(); //Must happen here!
        if (choice == 0)
        {
            atm.cashdp(amount);
            cout << "\tCustomer depositing $" << amount << endl;
        }
        else if (choice == 1)
        {
            atm.cashwd(amount);
            cout << "\tCustomer withdrawing $" << amount << endl;
        }
        else
            //error checker against the randomizer for the choice of depsoiting or withdrawing.
            cout << "error rand creating wrong number" << endl;
        rw.WriteUnlock(); //Must Happen here!
        Sleep(5000); // Sleeps the program between customer usage to mimic actual use.

    }
}
Community
  • 1
  • 1
  • Don't need any locks in observer because the atm.cashdp and atm.cashwd are write functions and need write lock. – Joseph Ryan Apr 18 '17 at 06:34