When to use recursive mutex?

后端 未结 6 599
旧时难觅i
旧时难觅i 2020-11-30 19:36

I understand recursive mutex allows mutex to be locked more than once without getting to a deadlock and should be unlocked the same number of times. But in what specific sit

6条回答
  •  再見小時候
    2020-11-30 20:18

    For example when you have function that calls it recursively, and you want to get synchronized access to it:

    void foo() {
       ... mutex_acquire();
       ... foo();
       ... mutex_release();
    }
    

    without a recursive mutex you would have to create an "entry point" function first, and this becomes cumbersome when you have a set of functions that are mutually recursive. Without recursive mutex:

    void foo_entry() {
       mutex_acquire(); foo(); mutex_release(); }
    
    void foo() { ... foo(); ... }
    

提交回复
热议问题