WEBVTT

00:06.030 --> 00:10.040
So guys, now let us start the implementation of the philosopher function.

00:10.050 --> 00:13.950
This function is the starting point of our solution, right?

00:13.950 --> 00:16.800
And this philosopher function is a threat function.

00:17.600 --> 00:23.780
That is, every thread which represents a philosopher will execute concurrently inside this function.

00:23.780 --> 00:24.560
Right?

00:24.830 --> 00:31.280
So to start with the implementation of this function, what is the argument that was passed to this

00:31.280 --> 00:32.090
function?

00:32.120 --> 00:36.800
The argument was simply a pointer to the philosopher, right?

00:36.830 --> 00:40.010
So let us typecast the argument into the philosopher.

00:40.010 --> 00:46.880
And now we already know that our philosopher thread was going to execute inside an infinite loop.

00:47.240 --> 00:48.080
Right?

00:48.170 --> 00:50.720
So that's why we have a while one loop.

00:50.720 --> 00:56.930
And now simply we will going to implement a solution at the highest level, right?

00:56.960 --> 01:05.330
So what I will going to do is that I will invoke an API which says that if philosopher get access to

01:05.330 --> 01:07.220
both the spoons, right.

01:07.220 --> 01:10.340
And I am passing the argument the pointer to the philosopher.

01:10.370 --> 01:16.010
This API returns true if the philosopher is able to get an access to both the spoons.

01:16.010 --> 01:16.760
Right.

01:17.060 --> 01:19.290
So what am I supposed to do here?

01:19.560 --> 01:21.120
I am inside the if block.

01:21.120 --> 01:28.560
It simply means that this particular philosopher has gotten access to both of his spoons successfully.

01:28.560 --> 01:29.310
Right?

01:29.430 --> 01:36.060
So the next step is quite obvious that this philosopher will now enjoy a meal or cake.

01:36.060 --> 01:36.960
Right?

01:37.320 --> 01:40.050
And we already have a sleep.

01:40.050 --> 01:43.890
One function inside this philosopher eat function.

01:43.890 --> 01:48.520
It simply means that philosopher will going to enjoy the meal for one second.

01:48.540 --> 01:54.090
So we satisfy a constraint that was given to us in our problem statement.

01:54.330 --> 01:55.170
Right.

01:55.320 --> 01:58.220
So the next step is again quite obvious.

01:58.230 --> 02:03.300
After enjoying the meal, the philosopher is supposed to release his both the spoons.

02:03.300 --> 02:04.120
Right.

02:04.140 --> 02:10.830
So hence I will invoke an API philosopher, release both the spoons and in this API, this philosopher

02:10.830 --> 02:14.460
is supposed to give up an access to both of his spoons.

02:14.490 --> 02:15.360
Right?

02:15.360 --> 02:23.280
And another constraint that we have was once the philosopher had a slice of the cake, then he should

02:23.280 --> 02:30.180
wait for one second in order to try his next attempt to grab the spoons again.

02:30.330 --> 02:36.270
So it is for this reason that as soon as the philosopher releases his spoons, we will make the philosopher

02:36.270 --> 02:37.710
sleep for one second.

02:37.740 --> 02:38.430
Right?

02:38.430 --> 02:43.380
So this is another constraint that our solution should satisfy, right?

02:44.870 --> 02:49.550
So you can see that this completes the implementation of the philosopher function.

02:49.580 --> 02:53.190
The things are pretty much quite obvious and simple.

02:53.210 --> 02:56.360
We are implementing the solution in a way.

02:56.360 --> 03:00.350
We think about the solution of a problem statement in our mind.

03:00.440 --> 03:01.220
Right?

03:02.990 --> 03:09.470
So this is how you should try to implement multithreaded programs by creating a layer of abstraction

03:09.470 --> 03:10.040
here.

03:10.040 --> 03:17.330
At this point of time, we have not yet discussed the implementation of these APIs, but we know what

03:17.330 --> 03:19.370
these APIs do, right?

03:19.370 --> 03:25.130
So we are able to code up our highest level function that is philosopher function using these supporting

03:25.130 --> 03:26.000
APIs.

03:26.240 --> 03:32.090
Now we will dive deep into the implementation of these two APIs a step by step, right?

03:32.330 --> 03:35.210
So this covers the first part of our solution.
