Multithreading 如何在单独的类函数c++;

Multithreading 如何在单独的类函数c++;,multithreading,visual-c++,Multithreading,Visual C++,我正在为一个班做最后一个项目。该项目模拟多个atm。这是我的程序已经运行了。在main.cpp中,我创建了线程,现在只有两个,以后可能会更多,如果客户要存款或取款,他们会调用一个类Begin that rand(),然后调用rand()他们要使用的金额,并执行5次 #include "ATM.h" void main() { Begin test1; test1.manager(); thread first(&Begin::atm, test1); th

我正在为一个班做最后一个项目。该项目模拟多个atm。这是我的程序已经运行了。在main.cpp中,我创建了线程,现在只有两个,以后可能会更多,如果客户要存款或取款,他们会调用一个类Begin that rand(),然后调用rand()他们要使用的金额,并执行5次

#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");
}
我不知道如何在observe()函数中挂起Main.cpp中创建的线程,如下所示:

    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

    }
}
void watcher::observe()
{
流动现金;
如果(resbox::cashbox->gettotal()>=resbox::cashbox->getmax())
{
//挂起所有其他线程

cout多亏了以下几点,我终于弄明白了我需要什么和使用什么:

通过利用这个类,在我的项目中。然后创建该类的全局实例

然后,我添加了读写器锁定和解锁,它在我的代码中的作用最好。如下所示:

  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.

    }
}
void Begin::atm()//使线程更容易执行的主函数
调用并运行程序。
{
ATM;
整数选择,数量;
大环;
QueryPerformanceCounter(&cicles);
srand(cicles.QuadPart);
对于(int i=0;icout多亏了以下几点,我终于弄明白了我需要什么和使用什么:

通过利用这个类,在我的项目中。然后创建该类的全局实例

然后,我添加了读写器锁定和解锁,它在我的代码中的作用最好。如下所示:

  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.

    }
}
void Begin::atm()//使线程更容易执行的主函数
调用并运行程序。
{
ATM;
整数选择,数量;
大环;
QueryPerformanceCounter(&cicles);
srand(cicles.QuadPart);
对于(int i=0;i显然,我需要创建一个共享互斥体,然后在atm函数中有一个读卡器锁定和解锁,在oberver函数中有一个写卡器锁定和解锁。我不完全确定如何使用互斥体。谢谢。显然,我需要创建一个共享互斥体,然后在atm函数中有一个读卡器锁定和解锁,还有一个oberver函数内部的writer lock unlock不完全确定如何使用互斥锁。感谢您在observer中不需要任何锁,因为atm.cashdp和atm.cashwd是写函数,需要写锁。在observer中不需要任何锁,因为atm.cashdp和atm.cashwd是写函数,需要写锁。