当前位置:网站首页>Hang wait lock vs spin lock (where both are used)

Hang wait lock vs spin lock (where both are used)

2022-07-05 05:24:00 abs(ln(1+NaN))

1、 Definition of spin lock

Hang wait lock : When a thread does not apply for a lock , At this point, the thread will be suspended , That is, join the waiting queue and wait . When the lock is released , Will be awakened , Re compete for locks

spinlocks : When a thread does not apply for a lock , The thread will not be suspended , Instead, check whether the lock is released at regular intervals . If the lock is released , Then competition lock ; If not released , Check again later .

2、 Spin lock correlation function

Linux A data type is provided to represent spin lock —— pthread_spinlock_t

(1) Lock / Unlock function

(2) initialization / Destroy function

 3、 Hang wait lock 、 Use scenario of spin lock

In that case , When should I use spin lock , When to use the pending lock ?

Example 1: You make an appointment with your friends to play basketball , Your friend said it would take an hour to get ready , So , You just went to the nearby Internet cafe for an hour to kill time .                

Example 2: You also ask your friends to play basketball , Your friend said 5 Minutes ready , So you wait downstairs , every other 1 Minutes to send a message to your friend asking about the situation .              

Example 1 ——》 Hang wait lock , When the critical zone operates for a long time , We usually use hang wait lock . Let's first let the thread PCB Join the waiting queue and wait , When the lock is released , Apply for the lock again .

Example 2 ——》 spinlocks , If you use a pending lock here , Maybe the thread just joined the waiting queue , The lock is released , It costs to wake up the thread . therefore , When the critical zone operates for a short time , We usually use spin lock .

原网站

版权声明
本文为[abs(ln(1+NaN))]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050520482666.html