0
我正在處理一個賦值,其中主函數創建了一個運行名爲run()
的函數的主線程。在run函數內部,我試圖使用一個客戶對象創建一個新線程(模擬客戶走到商店並離開)。一旦聲明瞭一個客戶對象,它就會運行一個模擬進入商店然後離開的人的功能。Thread.join()給我一個錯誤?
我在這裏有主要功能。在裏面,它聲明瞭一個執行run()
函數的主線程,在我試圖創建一個新線程的run函數內部,並且每次創建一個新線程時,都會創建一個新的客戶對象,並且客戶ID會增加。然後,我試圖執行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;
}
};
該程序在創建客戶對象時運行。在創建對象時,由於構造函數的原因,運行classCustomer.h
中的函數run()
。
我不明白爲什麼它不起作用。我會很感激的幫助。線程是很新的給我:/
你似乎並不被給你的線程的任何代碼運行。 – 2015-03-30 23:50:28
我只想指出,您給我們提供的錯誤的最佳描述是「錯誤」,我們甚至無法運行代碼來找到自己的錯誤。 – chris 2015-03-30 23:51:26
@JonathanPotter我爲這個問題增加了更多細節。看起來好像我還有一些比預期更多的錯誤:(抱歉沒有立即給出代碼,我在發佈後正在編輯它!感謝您的幫助!! – MR04 2015-03-30 23:57:43