-1

I'm trying to solve the dining philosophers problem and each time it's printing that only 2 are eating. Each thread I created was a philosopher and each section was a fork and according to the algorithm, each time we send a philosopher we try to get his forks(for the first it's fork1 and fork2) and the forks are the critical sections. Any idea on How to fix this? Here's my code:

    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <tchar.h>
    #include <iostream>
    #include <chrono>//To check runtime(it was also asked but I know how to do this)
    #include <thread>
    using namespace std;
    
    CRITICAL_SECTION ghCARITICALSection1;
    CRITICAL_SECTION ghCARITICALSection2;
    //Same for the rest
    DWORD WINAPI func(int* phiphilosopher)
    {
        if (1 == *phiphilosopher)
        {
            if (TryEnterCriticalSection(&ghCARITICALSection1)) {
                if (TryEnterCriticalSection(&ghCARITICALSection2)) {
                    cout << "1 is eating..."<< endl;
                    for (int i = 0; i < 1000000; i++)
                    {
                        i = i;
                    }
                    LeaveCriticalSection(&ghCARITICALSection2);
                }
                LeaveCriticalSection(&ghCARITICALSection1);
            }
        }
    //Same for the rest but with all the numbers increased and on the 5th we check 5 and 1

And that's the main:

    int main()
    {
        int philosopher1 = 1;
        int* philosopher1ptr = &philosopher1;
        //Same for the rest
    
        InitializeCriticalSection(&ghCARITICALSection1);
        InitializeCriticalSection(&ghCARITICALSection2);
//Same for the rest
    
        HANDLE WINAPI th1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, philosopher1ptr, 0, NULL);
//Same for the rest
    
        WaitForSingleObject(th1, INFINITE);
        //Same for the rest
    }
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
Velanar32
  • 57
  • 6
  • 1
    What is the dining philosophers problem? – Retired Ninja Jan 17 '21 at 16:17
  • Check here: [link](https://en.wikipedia.org/wiki/Dining_philosophers_problem) – Velanar32 Jan 17 '21 at 16:19
  • The description of your problem belongs directly in the question. – Retired Ninja Jan 17 '21 at 16:19
  • I don't think I entirely understand what do you mean. – Velanar32 Jan 17 '21 at 16:24
  • Well, if you try the problem with 5 philosophers, then at most 2 should be eating. – Phil1970 Jan 17 '21 at 17:08
  • As a new user here, please take the [tour] and read [ask]. In particular, you're not really describing a problem but ask how to solve it. Concerning your question, please provide a [mcve], even if it just means pasting the two snippets together. Do make sure that it doesn't include irrelevant stuff! BTW: Why don't you use C++ threads but the (nonportable) win32 API functions? – Ulrich Eckhardt Jan 17 '21 at 18:48
  • I know that at most 2 should be eating but it's only printing that 2 are eating and it skips the rest. When I run it I get " is eating..." while is what's being sent as a philosopher. I use the win32 API because it's a school assignment... – Velanar32 Jan 18 '21 at 00:11
  • This question has been asked before (see [here](https://stackoverflow.com/q/59814757/1889329), for example). It's neither well researched, or demonstrates even basic familiarity with the problem domain. The documentation for [CreateThread](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread) **explicitly** states that you cannot use it the way you are using it. – IInspectable Jan 18 '21 at 11:41
  • Speaking of not well researched, I did what was written there in my solution and I wrote it in my code yet it still happened. I understand that I'm using this the wrong way but this is what's required in the task. By the way, I really liked how instead of helping me and correcting the answer below that you said wasn't good you just directed people to the downvote button. – Velanar32 Jan 18 '21 at 15:11
  • Good to see that you like Stack Overflow's rules. If you want to learn more, take the [tour] which is customary for every new user. Oddly enough, you didn't. – IInspectable Jan 19 '21 at 09:14

1 Answers1

-2

probably two of the threads or more accessing to the same criticalSection and overlap eachother. try adding timer between each creation of a thread.

HANDLE WINAPI th1 = CreateThread(...);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
HANDLE WINAPI th2 = CreateThread(...);
  • why you use std `sleep` and winapi `CreateThread`? – apple apple Jan 17 '21 at 17:15
  • 2
    Sorry, but no: You can always shift the behaviour of multithreaded programs by adding delays, so that the likelihood various unwanted states are prevented, that's not a solution though. If you do things properly, you can do so without delays. – Ulrich Eckhardt Jan 17 '21 at 18:51
  • 1
    @ulr There's a downward pointing arrow next to this proposed answer. Its tooltip reads: "This answer isn't useful." After you rightfully identified this proposed answer as not being helpful, why did you not click the arrow to let other readers know? Is it the dreaded "This is a new contributor, so be gentle" bandwagon backfiring again? – IInspectable Jan 18 '21 at 11:36
  • @IInspectable, yes, kind-of: A bad answer is one thing, but a bad answer that is accepted as the correct answer is a whole other beast. I think this needs a bit of discussion. – Ulrich Eckhardt Jan 18 '21 at 15:17
  • @ulr Whether you rate a proposed answer as helpful or not may be subject to personal preference. This proposed answer, though, is actively harmful, in that it **seemingly** solves the issue. This is a ***MUST*** downvote contribution. No discussion required. – IInspectable Jan 18 '21 at 15:59
  • Chill, @IInspectable. I share your opinion mostly, but it's not yet a good question and and not a good answer. I still have a little bit of hope using a dialog. I'd rather close the question first though, since it still lacks in quality, so the answer becomes similarly invisible. Or, maybe, both are improved. – Ulrich Eckhardt Jan 18 '21 at 21:08