[英]Thread.join() gives me an error?
我正在做一個分配,其中主函數創建一個主線程,該主線程運行一個名為run()
的函數。 在運行功能中,我試圖使其成為一個帶有客戶對象的新線程(模擬客戶走到商店然后離開)。 聲明一個客戶對象后,它將運行一個函數,該函數模擬進店然后離開的人。
我在這里有主要功能。 在內部,它聲明一個執行run()
函數的主線程,在我試圖創建一個新線程的run函數內部,並且每次創建一個新線程時,也會創建一個新的customer對象,並且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
的函數run()
。
我不明白為什么它不起作用。 我將不勝感激。 線程對我來說很新:/
在退出main()之前,請先加入內螺紋:
int main()
{
thread newThread(run);
newThread.join(); // <-- missing line
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.