Advantages of using condition variables over mutex

前端 未结 3 866
我寻月下人不归
我寻月下人不归 2020-12-22 19:22

I was wondering what is the performance benefit of using condition variables over mutex locks in pthreads.

What I found is : \"Without condition variables, the prog

3条回答
  •  滥情空心
    2020-12-22 20:29

    If you are looking for performance, then start reading about "non blocking / non locking" thread synchronization algorithms. They are based upon atomic operations, which gcc is kind enough to provide. Lookup gcc atomic operations. Our tests showed we could increment a global value with multiple threads using atomic operation magnitudes faster than locking with a mutex. Here is some sample code that shows how to add items to and from a linked list from multiple threads at the same time without locking.

    For sleeping and waking threads, signals are much faster than conditions. You use pthread_kill to send the signal, and sigwait to sleep the thread. We tested this too with the same kind of performance benefits. Here is some example code.

提交回复
热议问题