title: Dining philosophers problem - only 2 thread worked
body: <p>I am trying to solve the <a href="https://en.wikipedia.org/wiki/Dining_philosophers_problem" rel="nofollow noreferrer">dining philosophers problem</a>.</p>
  
  <p>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:</p>
  
  <pre><code>CRITICAL_SECTION ghCARITICALSection1;
  CRITICAL_SECTION ghCARITICALSection2;
  CRITICAL_SECTION ghCARITICALSection3;
  CRITICAL_SECTION ghCARITICALSection4;
  CRITICAL_SECTION ghCARITICALSection5;
  DWORD WINAPI func(int* phiphilosopher)
  {
      if (1 == *phiphilosopher &amp;&amp; TryEnterCriticalSection(&amp;ghCARITICALSection1) &amp;&amp; TryEnterCriticalSection(&amp;ghCARITICALSection2))
      {
          std::cout &lt;&lt; "1 is eating...\n";
          for (int i = 0; i &lt; 1000000; i++)
          {
              i = i;
          }
          LeaveCriticalSection(&amp;ghCARITICALSection1);
          LeaveCriticalSection(&amp;ghCARITICALSection2);
      }
      if (2 == *phiphilosopher &amp;&amp; TryEnterCriticalSection(&amp;ghCARITICALSection2) &amp;&amp; TryEnterCriticalSection(&amp;ghCARITICALSection3))
      {
          std::cout &lt;&lt; "2 is eating...\n";
          for (int i = 0; i &lt; 1000000; i++)
          {
          }
          LeaveCriticalSection(&amp;ghCARITICALSection2);
          LeaveCriticalSection(&amp;ghCARITICALSection3);
      }
      if (3 == *phiphilosopher &amp;&amp; TryEnterCriticalSection(&amp;ghCARITICALSection3) &amp;&amp; TryEnterCriticalSection(&amp;ghCARITICALSection4))
      {
          std::cout &lt;&lt; "3 is eating...\n";
          for (int i = 0; i &lt; 1000000; i++)
          {
          }
          LeaveCriticalSection(&amp;ghCARITICALSection3);
          LeaveCriticalSection(&amp;ghCARITICALSection4);
      }
      //...also for 4,5
      return 0;
  }
  </code></pre>
  
  <pre><code>    int philosopher1 = 1;
      int* philosopher1ptr = &amp;philosopher1;
      int philosopher2 = 2;
      int* philosopher2ptr = &amp;philosopher2;
      //...Also for philosopher 3,4,5
  
      InitializeCriticalSection(&amp;ghCARITICALSection1);
      InitializeCriticalSection(&amp;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
  </code></pre>
  
  <ul>
  <li>Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it is not being used by another philosopher. </li>
  </ul>