Thread.join()给我一个错误?

时间:2015-03-30 23:48:46

标签: c++ multithreading

我正在进行一项赋值,其中main函数创建一个运行名为run()的函数的主线程。在run函数中,我正在尝试使用一个客户对象创建一个新线程(模拟客户走到商店然后离开)。一旦声明了一个客户对象,它就会运行一个模拟进入商店然后离开的人的功能。

我这里有主要功能。在里面,它声明了一个执行run()函数的主线程,在run函数中我试图创建一个新线程,每次创建一个新线程时,都会创建一个新的客户对象, customerID递增。然后我尝试实现newCustThread.join(),以便前一个客户线程在while循环继续之前完成,并创建下一个客户线程。

主要

#include <iostream>
#include <thread>
#include "classBarberShop.h"
#include "classCustomer.h"
using namespace std;

void run();
void createCustomerObj(int customerID, BarberShop newShop);



int WAIT_TIME = 3;
BarberShop newShop();
int customerID = 1;

int main(){
    thread newThread(run);

    return 0;
}

void run(){
    while (customerID <= 5){
        thread newCustThread(Customer newCustomer(int customerID, BarberShop newShop));
        newCustThread.join(); //    <===== ERROR HERE

        customerID++;
    }
    return;
}

classBarberShop.h

#include <iostream>
using namespace std;

enum bState {
    FULL = -1,
    EMPTY = 0,
    OCCUPIED = 1,
    SLEEPING = 2,
    DONE = 3,
    WAITING = 4
};

class BarberShop {

public:
    BarberShop(){
        chairNum = 5;
        barber = SLEEPING;

        for (int i = 0; i < chairNum; i++){
            chairState[i] = EMPTY;
        }
    }

    bool findChair(int passingCustomer){
        int getEmptyChair = getFirstEmptyChair();

        if (getEmptyChair == FULL){
            return false;
        }
        else {
            chairState[getEmptyChair] = OCCUPIED;
        }

        return true;
    }

    int getHairCut(int customer){
        if (barber == SLEEPING){

            barber = OCCUPIED;
            return SLEEPING;
        }
        else if (barber == OCCUPIED){
            bool chairFound = findChair(customer);

            if (chairFound == false){
                return FULL;
            }
            else{
                /*while (barber == OCCUPIED){

                }*/

                for (int i = 0; i < chairNum; i++){
                    if (chairState[i] == OCCUPIED){
                        chairState[i] = EMPTY;
                        break;
                    }
                }

                barber = OCCUPIED;
                return WAITING;
            }
        }
        else{
            barber = OCCUPIED;
            return DONE;
        }
    }

    int leaveBarberShop(int customer){
        bool isWaiting = isAnyoneWaiting();

        if (isWaiting == true){
            barber = DONE;
        }
        else{
            barber = SLEEPING;
        }

        //notify();
    }

    int getFirstEmptyChair(){
        for (int i = 0; i < chairNum; i++){
            if (chairState[i] == EMPTY){
                return i;
            }

            return FULL;
        }
    }

    bool isAnyoneWaiting(){
        for (int i = 0; i < chairNum; i++){
            if (chairState[i] == OCCUPIED){
                return true;
            }
        }

        return false;
    }

//private:
    int chairNum;
    int barber;
    int chairState[5];
};

classCustomer.h

#include <iostream>
#include "classBarberShop.h"
using namespace std;

class Customer {

    int customer;
    BarberShop shop;
    int bstate;
    int HAIRCUT_TIME = 5;

    Customer(int passingCustomer, BarberShop passingShop) {
        shop = passingShop;
        customer = passingCustomer;
        run();
    }

    void run() {
        int sleepingTime = (int)(HAIRCUT_TIME * rand());

        cout << "ENTERING SHOP: Customer [" << customer << "] entering barber shop for haircut." << endl;

        bstate = OCCUPIED;
        bstate = shop.getHairCut(customer);

        if (bstate == WAITING){
            cout << "Barber's busy: Customer [" << customer << "] has waited and now wants a haircut." << endl;
        }
        else if (bstate == SLEEPING){
            cout << "Barber's asleep: Customer [" << customer << "] is waking him up and getting a haircut." << endl;
        }
        else if (bstate == FULL){
            cout << "Barber shop is full: Customer [" << customer << "] is leaving shop." << endl;
            return;
        }
        else {
            cout << "HAIRCUT: Customer [" << customer << "] is getting haircut." << endl;
        }

        //******Suspend thread here?
        cout << "LEAVING SHOP: Customer [" << customer << "] haircut finished: leaving shop." << endl;
        bstate = shop.leaveBarberShop(customer);

        return;
    }
};

创建客户对象时会运行程序。当您创建对象时,由于构造函数,run()中的函数classCustomer.h正在运行。

我不明白为什么它不起作用。我将非常感谢你的帮助。线程对我来说很新鲜:/

1 个答案:

答案 0 :(得分:1)

在退出main()之前加入内部线程:

int main()
{
   thread newThread(run);
   newThread.join();  // <-- missing line
   return 0;
}