Practical example for pthread_mutex_trylock

  Рет қаралды 16,292

CodeVault

CodeVault

Күн бұрын

Пікірлер: 35
@muhammetcavus6995
@muhammetcavus6995 3 жыл бұрын
This series really saved me from watching boring teaching assistant videos. Greatly explained. THANK YOU A LOT.
@onurcanisler
@onurcanisler 2 жыл бұрын
*LoL I agree with that:DDD*
@subbu4u
@subbu4u Жыл бұрын
Very very good explanation...complete series is awesome
@narasarajv5278
@narasarajv5278 3 жыл бұрын
Interesting concept for explaining the program...Thank you..
@katiabibi1966
@katiabibi1966 3 жыл бұрын
very well explained, thank you. But I do not understand why we have "no stove available" in the beginning of execution.. aren't all stoves available at that time?
@CodeVault
@CodeVault 3 жыл бұрын
Ahh it's just the order of printing. Since we have 4 stoves and 10 cooks the first 4 get a stove (but they don't print anything) and the other 6 print out "No stove available", and only after 500ms you'll see the 4 cooks that got a stove print something. You could add the message "Started cooking..." right before: stoveFuel[i] -= fuelNeeded; usleep(500000); And should be clear
@katiabibi1966
@katiabibi1966 3 жыл бұрын
@@CodeVault thank you so much for the answer !!!
@probexpd1916
@probexpd1916 Жыл бұрын
@@CodeVault Thank you for that answer, Sergui! I keep forgetting that with threads, the sequence of the display doesn't always correlate with what's actually happening. I added "usleep(900000)" like this... usleep(900000); printf("No stove available, wait... "); usleep(300000); Now, the output shows 4 stoves being used, 6 cooks waiting, 4 more stoves being used, 2 cooks waiting and 2 stoves being used _in sequence_
@mustafa7891
@mustafa7891 3 жыл бұрын
Nicely explained, it was very helpful : ) But what if a chef occupies a stove that does not have enough fuel, while some other stove have enough fuel for the chef? The chef will still go home, right?
@CodeVault
@CodeVault 3 жыл бұрын
Yea, that's what would happen. You can try and create a mechanism to try other stoves if one is empty of fuel. I kept it simple.
@prashantpatil3088
@prashantpatil3088 3 жыл бұрын
Nicely explain. Please make a video on Shared memory, Message Queue
@CodeVault
@CodeVault 3 жыл бұрын
They are on the to-do list ;)
@prashantpatil3088
@prashantpatil3088 3 жыл бұрын
@@CodeVault thanks bro.
@atjhamit
@atjhamit 2 жыл бұрын
Thanks a lot! Really appreciate the help.
@maxtirdatov
@maxtirdatov 3 жыл бұрын
Shouldn't i be reset to -1 after sleeping? Otherwise the stove 0 is ignored because i gets incremented before the next iteration.
@CodeVault
@CodeVault 3 жыл бұрын
Ah, you're right, that's a small mistake I made. My bad
@JamesPetts
@JamesPetts 5 ай бұрын
I approve of cooking pots.
@yahmk3978
@yahmk3978 Жыл бұрын
Thank you!
@iliqnemilov6091
@iliqnemilov6091 2 жыл бұрын
great video man,thanks a lot
@denizbatu7812
@denizbatu7812 2 жыл бұрын
excellent
@beingnikhil155
@beingnikhil155 3 жыл бұрын
Is sleeping for some milli seconds a good practice if all the stoves are busy or we can do something better?
@CodeVault
@CodeVault 3 жыл бұрын
Definitely something better can be done. Like linking it to a condition variable and sleep until someone signals that they stopped using the stove.
@muhammetcavus6995
@muhammetcavus6995 3 жыл бұрын
when if(stovefuel[i]-fuelneeded
@CodeVault
@CodeVault 3 жыл бұрын
Yes, you can. Simply unlock the mutex and then continue: pthread_mutex_unlock(&stoveMutex[i]); continue;
@Codality
@Codality 2 жыл бұрын
THE BEST
@Pyrografpl
@Pyrografpl 2 жыл бұрын
Thank you mate :-D
@HaroldX7677
@HaroldX7677 7 ай бұрын
Maybe i should be set to -1 so that the loop will start again from 0?
@CodeVault
@CodeVault 7 ай бұрын
Which of the loops?
@qrutyq
@qrutyq 2 ай бұрын
Yep it should!
@qrutyq
@qrutyq 2 ай бұрын
@@CodeVault in the routine one. In other case it will ignore stove with index 0.
@user-yz7ts2fq9m
@user-yz7ts2fq9m 3 жыл бұрын
Super!
@sagivalia5041
@sagivalia5041 2 жыл бұрын
So trylock can be useful if there are more than one critical sections of the same type, right?
@CodeVault
@CodeVault 2 жыл бұрын
Yes, that's one way to use it
@kippie80
@kippie80 2 жыл бұрын
Would like to see these examples applied to some GPUs.
@CodeVault
@CodeVault 2 жыл бұрын
Using CUDA/OpenCL?
What is pthread_exit?
6:20
CodeVault
Рет қаралды 18 М.
Practical example to barriers (pthread_barrier)
16:41
CodeVault
Рет қаралды 14 М.
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 10 МЛН
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 62 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 66 МЛН
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 851 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 276 М.
Thread Pools in C (using the PTHREAD API)
22:54
CodeVault
Рет қаралды 44 М.
Deadlocks in C
10:59
CodeVault
Рет қаралды 27 М.
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 947 М.
Harder Than It Seems? 5 Minute Timer in C++
20:10
The Cherno
Рет қаралды 213 М.
Writing Code That Runs FAST on a GPU
15:32
Low Level
Рет қаралды 567 М.
Condition variables in C
18:57
CodeVault
Рет қаралды 98 М.
What is Mutex in C++ (Multithreading for Beginners)
12:29
CodeBeauty
Рет қаралды 46 М.
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 10 МЛН