Clean Code


Figure A-3 338 Appendix A: Concurrency II



Download 3,58 Mb.
Pdf ko'rish
bet314/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   310   311   312   313   314   315   316   317   ...   384
Bog'liq
Clean Code

Figure A-3


338
Appendix A: Concurrency II
Breaking Lock & Wait
You can also eliminate deadlock if you refuse to wait. Check each resource before you
seize it, and release all resources and start over if you run into one that’s busy. 
This approach introduces several potential problems:

Starvation—One thread keeps being unable to acquire the resources it needs (maybe it 
has a unique combination of resources that seldom all become available).

Livelock—Several threads might get into lockstep and all acquire one resource and 
then release one resource, over and over again. This is especially likely with simplistic 
CPU scheduling algorithms (think embedded devices or simplistic hand-written 
thread balancing algorithms).
Both of these can cause poor throughput. The first results in low CPU utilization,
whereas the second results in high and useless CPU utilization. 
As inefficient as this strategy sounds, it’s better than nothing. It has the benefit that it
can almost always be implemented if all else fails. 
Breaking Preemption
Another strategy for avoiding deadlock is to allow threads to take resources away from
other threads. This is usually done through a simple request mechanism. When a thread
discovers that a resource is busy, it asks the owner to release it. If the owner is also waiting
for some other resource, it releases them all and starts over. 
This is similar to the previous approach but has the benefit that a thread is allowed to
wait for a resource. This decreases the number of startovers. Be warned, however, that
managing all those requests can be tricky. 
Breaking Circular Wait
This is the most common approach to preventing deadlock. For most systems it requires
no more than a simple convention agreed to by all parties.
In the example above with Thread 1 wanting both Resource 1 and Resource 2 and
Thread 2 wanting both Resource 2 and then Resource 1, simply forcing both Thread 1 and
Thread 2 to allocate resources in the same order makes circular wait impossible.
More generally, if all threads can agree on a global ordering of resources and if they
all allocate resources in that order, then deadlock is impossible. Like all the other strate-
gies, this can cause problems:

The order of acquisition might not correspond to the order of use; thus a resource 
acquired at the start might not be used until the end. This can cause resources to be 
locked longer than strictly necessary.


339

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   310   311   312   313   314   315   316   317   ...   384




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish