join()给我一个错误
Thread.join() gives me an error?
我正在处理一个赋值,其中主函数创建一个主线程,该线程运行一个名为run()
的函数。在run函数中,我试图使它与customer对象一起创建一个新线程(模拟顾客走向商店并离开)。一旦声明了一个客户对象,它就会运行一个函数,模拟进入商店然后离开的人。
我这里有主要功能。在内部,它声明了一个执行run()
函数的主线程,在run函数内部,我正试图创建一个新线程,每次创建新线程时,都会创建一个全新的客户对象,并且customerID会递增。然后,我尝试实现newCustThread.join()
,以便在while循环继续并创建下一个客户线程之前,上一个客户螺纹完成。
Main:
#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()
将运行。
我不明白为什么它不起作用。我将感谢你的帮助。线程对我来说是非常新的:/
在退出main()之前加入内线程:
int main()
{
thread newThread(run);
newThread.join(); // <-- missing line
return 0;
}
相关文章:
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- 在Cython中使用C库时,我遇到了一个错误
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 我试图用c++编写递归fibonacci序列,但当我编译时,我遇到了一个错误
- 从system()调用G++会返回一个错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 使用声明:GCC 和 Clang 的另一个错误?
- 全球免费给出一个错误.调试器不解释
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
- 为什么第三板有一个错误
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 我需要在 Android Studio 中构建 NDK. 但它返回一个错误