本文介绍了用餐哲学家问题 - 只有 2 个线程有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决用餐哲学家问题.

就我而言,每个哲学家都应该吃 1,000,000 次.问题是它似乎只有1",而3"吃完了.我正在使用带有临界区锁的线程,这是我的代码:

In my case, every philosopher should eat 1,000,000 times.The problem is that it seems like only "1" and is "3" finished eating.I am using threads with critical section lock, here is my code:

CRITICAL_SECTION ghCARITICALSection1;
CRITICAL_SECTION ghCARITICALSection2;
CRITICAL_SECTION ghCARITICALSection3;
CRITICAL_SECTION ghCARITICALSection4;
CRITICAL_SECTION ghCARITICALSection5;
DWORD WINAPI func(int* phiphilosopher)
{
    if (1 == *phiphilosopher && TryEnterCriticalSection(&ghCARITICALSection1) && TryEnterCriticalSection(&ghCARITICALSection2))
    {
        std::cout << "1 is eating...\n";
        for (int i = 0; i < 1000000; i++)
        {
            i = i;
        }
        LeaveCriticalSection(&ghCARITICALSection1);
        LeaveCriticalSection(&ghCARITICALSection2);
    }
    if (2 == *phiphilosopher && TryEnterCriticalSection(&ghCARITICALSection2) && TryEnterCriticalSection(&ghCARITICALSection3))
    {
        std::cout << "2 is eating...\n";
        for (int i = 0; i < 1000000; i++)
        {
        }
        LeaveCriticalSection(&ghCARITICALSection2);
        LeaveCriticalSection(&ghCARITICALSection3);
    }
    if (3 == *phiphilosopher && TryEnterCriticalSection(&ghCARITICALSection3) && TryEnterCriticalSection(&ghCARITICALSection4))
    {
        std::cout << "3 is eating...\n";
        for (int i = 0; i < 1000000; i++)
        {
        }
        LeaveCriticalSection(&ghCARITICALSection3);
        LeaveCriticalSection(&ghCARITICALSection4);
    }
    //...also for 4,5
    return 0;
}
    int philosopher1 = 1;
    int* philosopher1ptr = &philosopher1;
    int philosopher2 = 2;
    int* philosopher2ptr = &philosopher2;
    //...Also for philosopher 3,4,5

    InitializeCriticalSection(&ghCARITICALSection1);
    InitializeCriticalSection(&ghCARITICALSection2);
//...aslo for ghCARITICALSection 3,4,5

    HANDLE WINAPI th1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, philosopher1ptr, 0, NULL);
    HANDLE WINAPI th2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, philosopher2ptr, 0, NULL);
////...aslo for th3,4,5
    WaitForSingleObject(th1, INFINITE);
    WaitForSingleObject(th2, INFINITE);
    //...also for th3,4,5

  • 每位哲学家都必须交替思考和进食.但是,哲学家只有在左叉和右叉同时拥有时才能吃意大利面.每个叉子只能由一个哲学家持有,因此只有当叉子没有被其他哲学家使用时,哲学家才能使用它.
  • 推荐答案

    想想这里的逻辑

        if (TryEnterCriticalSection(&a) && TryEnterCriticalSection(&b)) {
            // . . .
            LeaveCriticalSection(&a);
            LeaveCriticalSection(&b);
        }
    

    如果 TryEnterCriticalSection(&a) 成功而 TryEnterCriticalSection(&b) 失败会发生什么;CS a 永远保持进入状态.

    What happens if TryEnterCriticalSection(&a) succeeds and TryEnterCriticalSection(&b) fails; the CS a remains in entered state forever.

    它应该看起来像

        if (TryEnterCriticalSection(&a)) {
            if (TryEnterCriticalSection(&b)) {
                // . . .
                LeaveCriticalSection(&b);
            }
            LeaveCriticalSection(&a);
        }
    

    这篇关于用餐哲学家问题 - 只有 2 个线程有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:57