So guys, now let us start the implementation of the philosopher function.
This function is the starting point of our solution, right?
And this philosopher function is a threat function.
That is, every thread which represents a philosopher will execute concurrently inside this function.
Right?
So to start with the implementation of this function, what is the argument that was passed to this
function?
The argument was simply a pointer to the philosopher, right?
So let us typecast the argument into the philosopher.
And now we already know that our philosopher thread was going to execute inside an infinite loop.
Right?
So that's why we have a while one loop.
And now simply we will going to implement a solution at the highest level, right?
So what I will going to do is that I will invoke an API which says that if philosopher get access to
both the spoons, right.
And I am passing the argument the pointer to the philosopher.
This API returns true if the philosopher is able to get an access to both the spoons.
Right.
So what am I supposed to do here?
I am inside the if block.
It simply means that this particular philosopher has gotten access to both of his spoons successfully.
Right?
So the next step is quite obvious that this philosopher will now enjoy a meal or cake.
Right?
And we already have a sleep.
One function inside this philosopher eat function.
It simply means that philosopher will going to enjoy the meal for one second.
So we satisfy a constraint that was given to us in our problem statement.
Right.
So the next step is again quite obvious.
After enjoying the meal, the philosopher is supposed to release his both the spoons.
Right.
So hence I will invoke an API philosopher, release both the spoons and in this API, this philosopher
is supposed to give up an access to both of his spoons.
Right?
And another constraint that we have was once the philosopher had a slice of the cake, then he should
wait for one second in order to try his next attempt to grab the spoons again.
So it is for this reason that as soon as the philosopher releases his spoons, we will make the philosopher
sleep for one second.
Right?
So this is another constraint that our solution should satisfy, right?
So you can see that this completes the implementation of the philosopher function.
The things are pretty much quite obvious and simple.
We are implementing the solution in a way.
We think about the solution of a problem statement in our mind.
Right?
So this is how you should try to implement multithreaded programs by creating a layer of abstraction
here.
At this point of time, we have not yet discussed the implementation of these APIs, but we know what
these APIs do, right?
So we are able to code up our highest level function that is philosopher function using these supporting
APIs.
Now we will dive deep into the implementation of these two APIs a step by step, right?
So this covers the first part of our solution.