pthread_cond_wait versus semaphore

前端 未结 4 1661
慢半拍i
慢半拍i 2020-12-04 06:22

What are the pros / cons of using pthread_cond_wait or using a semaphore ? I am waiting for a state change like this :

pthread_mutex_lock(&c         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-04 06:53

    In your second snippet, you're getting the lock multitude of times, never releasing it.

    In general, the state you're waintin on can be completely expressed by a semaphore, then you can use just that. A lock structure is smaller in size, and it requires less atomic operations to check/set/release.

    Otherwise, if the state is complex, and different parts of the code wait on different conditions of the same variable (eg, here you want x<10; there you want y>x), use cond_wait.

提交回复
热议问题