- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个程序,该程序使用线程充当尝试入住、使用和退房酒店的客人。
为了跨所有线程跟踪 guest 数据,我使用了一个struct
:
struct thread_data
{
int guest_id; // id number of guest
int room; // room guest utilizes
int event; // 0=swim, 1=resturaunt, 2=fitness, 3=business
int cost; // final cost of stay (random value)
}
由于会有多个客人使用酒店,我尝试创建一个客人结构数组:
struct thread_data thread_data_array[NUM_GUESTS]; // array initialization
问题 1:这是否正确创建了 struct
类型的数组?数组的名称是thread_data_array
吗?
此外,我有我的 Guest()
方法,在该方法中我尝试从该 struct
获取和设置数据:
void *Guest(void *guestData)
{
int guestid, guestroom, gevent, gcost;
struct thread_data *guest_data;
guestroom = guest_data->room;
gevent = getRandomLessThan(4); // activity connected to 0, 1, 2, 3
guest_data[guestid].event = gevent; // assign to Guest's struct
}
问题 2:我是否正确地从我的结构中“获取”和“设置”?
简而言之,我对我的三个部分的正确命名有点困惑:
结构:struct thread_data;
数组:struct thread_data thread_data_array[NUM_GUESTS];
在 Main() 中:struct thread_data *guest_data;
这是我的整个程序,包括所有错误:
//Hotel Project
//This project creates a system to simulate a hotel.
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_GUESTS 10 //10 guests
void *CheckIn(void *); // the two threads
void *CheckOut(void *);
pthread_t guests[NUM_GUESTS];
sem_t roomSem; //total number of rooms in the hotel
sem_t check_in, check_out; //reception desks
sem_t s1, s2, s3, s4, s5; //global semaphores
int rooms[5]; //keep track of availability of each room
int total_guests; //incremented with each guest check-out
int pool; //incremented with each guest use pool
int resturant; //incremented with each guest use resturant
int fitness_center; //incremented with each guest use fitness center
int business_center; //incremented with each guest use business center
struct thread_data
{
int guest_id; // id number of guest
int room; // room guest utilizes
int event; // 0=swim, 1=resturaunt, 2=fitness, 3=business
int cost; // final cost of stay (random value)
}
struct thread_data thread_data_array[NUM_GUESTS]; // array initialization
void *Guest(void *guestData)
{
int guestNumber = *(int *) guestData; //error?
int guestid, guestroom, gevent, gcost;
struct thread_data *guest_data;
guestid = (struct int) guestData; //error?
//guest_data = (struct thread_data *) guestData;
//guestid = guest_data->guest_id; // guestID stored in the struct
/*output*/ printf("Guest %d waits for room to be available\n", guestid);
sem_wait(&roomSem); // guest wants a room; check to see if one is available
/*output*/ printf("Guest %d waits for check-in\n", guestid);
sem_wait(&check_in); // if there is a room available, guest approaches check_in clerk; making him busy
/*output*/ printf("Guest %d goes to the check-in reservationist\n", guestid);
CheckIn(thread_data_array[guestid]); //check in
sem_post(&s1); // event ordering
sem_wait(&s2);
guestroom = guest_data->room;
/*output*/ printf("Guest %d receives Room %d and completes check-in\n", guestid, room);
sem_post(&check_in); // event ordering. Check-In now available
gevent = getRandomLessThan(4); // activity connected to 0, 1, 2, 3
guest_data[guestid].event = gevent; // assign to Guest's struct
switch(gevent)
{
case(0): /*output*/ printf("Guest %d does event: Pool\n", guestid);
pool++;
case(1): /*output*/ printf("Guest %d does event: Resturant\n", guestid);
resturant++;
case(2): /*output*/ printf("Guest %d does event: Fitness Center\n", guestid);
fitness_center++;
case(3): /*output*/ printf("Guest %d does event: Business Center\n", guestid);
business_center++;
}
/*output*/ printf("Guest %d does event: %d\n", guestid, gevent);
sleep(getRandomLessThan(5) - 1); // sleep for 1 to 3 seconds
gcost = getRandomLessThan(300); // 0 < cost < 300
guest_data[guestid].cost = gcost; // maybe should be in CheckOut
//thread_data_array[guest].cost = money;
sem_wait(&check_out); // guest waits for check-out desk to be available
/*output*/ printf("Guest %d goes to the check-out reservationist and returns room %d\n", guestid, room);
sem_post(&s3); // event ordering
printf("Main: Creating CheckOut thread %d", guestid);
rc = pthread_create(&guest[guestid], NULL, CheckOut, (void *)&thread_data_array[guest]);
if (rc){
printf("ERROR1; return code from pthread_create() is %d\n", rc);
exit(-1);
}
sem_wait(&s4); // event ordering
total = thread_data_array[guestid].cost;
/*output*/ printf("Guest %d receives the total balance of $%d\n", guestid, total);
/*output*/ printf("Guest %d makes a payment\n", guestid);
total_guests++;
sem_post(&s5); // event ordering
}
int getRandomLessThan(int x)
{
srand(time(NULL));
int r = rand() % x;
printf("Random number: %d\n", r);
return r;
}
int GetOpenRoom()
{
for (int i=0; i<rooms.length; i++)
{
if (rooms[i] = 1) // room vacant
{
rooms[i] = 0; // mark as filled
return i;
}
}
}
void *CheckIn(void *guestData)
{
int guestid, guestroom;
struct thread_data *guest_data;
guest_data = (struct thread_data *) guestData;
guestid = guest_data->guest_id; // guestID stored in the struct
/**/guestroom = guest_data->room; // room stored in the struct
/**/printf("Guestroom: %d", guestroom); // expecting NULL
sem_wait(&s1); // event ordering
/*output*/ printf("The check-in reservationist greets Guest %d\n", guestid);
sem_wait(&roomSem); // use a room
guestroom = GetOpenRoom();
guest_data[guestid].room = guestroom; // mark room number in guest's struct
//thread_data_array[guestid].room = guestroom;
//which do i need?
/*output*/ printf("Assign room %d to Guest %d\n", guestroom, guestid);
sem_post(&s2); // event ordering
pthread_exit(NULL);
}
void *CheckOut(void *guestData)
{
int guestid, guestroom, guestcost;
struct thread_data *guest_data;
guest_data = (struct thread_data *) guestData;
guestid = guest_data->guest_id; // guestID stored in the struct
guestroom = guest_data->room; // room stored in the struct
guestcost = guest_data->cost; // cost stored in the struct
sem_wait(&s3); // event ordering
/*output*/ printf("The check-out greets Guest %d and receives the key from room %d\n", guestid, guestroom);
rooms[guestroom] = 1; // room now available, set back to 1
sem_post(&roomSem); // release a room
/*output*/ printf("Calculate the balance for Guest %d\n", guestid);
//case situation for each event. makes guestcost= different value for each event
thread_data_array[guestid].cost = guestcost;
sem_post(&s4); // event ordering
sem_wait(&s5); // event ordering
/*output*/ printf("Receive $%d from Guest %d and complete the check-out\n", cost, guestid);
pthread_exit(NULL);
}
//Guest
int main (int argc, char *argv[])
{
pthread_t desk[2];
int rc;
int guest;
struct thread_data *guest_data;
sem_init(&roomSem, 1, 5); //not 0 means shared between multiple processes, 5 is initial value
sem_init(&check_in, 1, 1);
sem_init(&check_out, 1, 1);
sem_init(&s1, 1, 0);
sem_init(&s2, 1, 0);
sem_init(&s3, 1, 0);
sem_init(&s4, 1, 0);
sem_init(&s5, 1, 0);
rooms[5] = {1,1,1,1,1};
pthread_create(&desk[0], NULL, CheckIn, NULL); // Check In runs on it's own now
pthread_create(&desk[1], NULL, CheckOut, NULL); // Check Out runs on it's own now
for(guest = 0; guest < NUM_GUESTS; guest++)
{
int *guestNumber = malloc(sizeof(*guestNumber);
guestNumber = guest;
//thread_data_array[guest].guest_id = guest; // guest ID for each guest thread
rc = pthread_create(guests[guest], NULL, Guest, guestNumber); //Guest method, guest argument
//Guest only takes a void argument. Must use thread_data_array?
if (rc) {
printf("ERROR; return code from pthread_create(Guest) is %d\n", rc);
exit(-1);
}
}
printf("\t\t\tNumber of Customers\n");
printf("Total Guests:\t%d\n", total_guests);
printf("Pool:\t%d\n", pool);
printf("Resturant:\t%d\n", resturant);
printf("Fitness Center:\t%d\n", fitness_center);
printf("Business Center:\t%d\n", business_center);
sem_close(&roomSem);
sem_close(&check_in);
sem_close(&check_out);
sem_close(&s1);
sem_close(&s2);
sem_close(&s3);
sem_close(&s4);
sem_close(&s5);
pthread_exit(NULL);
}
编辑:我在线程中使用空指针作为参数,因为我使用的是 pthread_create()
最佳答案
问题 1:
是的,struct thread_data thread_data_array[NUM_GUESTS];
正在创建 thread_data_array
作为 NUM_GUESTS
的数组类型元素 struct thread_data
.
问题 2:
guest_data
是指向您的结构的指针,但未初始化(指向任何地方):
struct thread_data *guest_data;
guestroom = guest_data->room;
关于c - 正确使用结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36874809/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!