试图通过创建一个门卫来解决哲学家的餐饮问题,只允许4位哲学家一次性用餐,计划使用信号量但是关于它们的材料有限网络,我不知道如何增加信号量的值,一旦它已被发信号。如何增加C++中的信号量值来解决哲学家用餐
#define INITIAL_COUNT 1
#define MAX_COUNT 4
主()
philo.doorSemaphore = CreateSemaphore(
NULL, //default security attributes
INITIAL_COUNT, //initial count
MAX_COUNT, //maximum count
NULL);
while (philo.not_dead == true)
{
int num_philosophers = 5;
for (int i = 0; i < 5; i++)
{
philo.mythread[i] = thread (philosophersFunction, i); //init 5 threads calling philofunction each loop
philo.mythread[i].join(); //join thread to current thread each loop
}
sleep_for(milliseconds(500));
system("cls");
}
等待()
void Philosophers::waiting(int current)
{
dWaitResult = WaitForSingleObject(doorSemaphore, 0L);
//waitResult = WaitForSingleObject(semaphores, 0L);
switch (dWaitResult)
{
case WAIT_OBJECT_0:
p[current] = hungry;
ReleaseSemaphore(doorSemaphore, 1, NULL);
break;
case WAIT_TIMEOUT:
hunger[current] ++;
counter[current] ++;
case WAIT_FAILED :
break;
CloseHandle(doorSemaphore);
}
}
您正在将Windows WinAPI线程库函数与C++标准线程库混合使用。从标准中选择一些:http://en.cppreference.com/w/cpp/thread – marcinj