Making pthread_rwlock_wrlock recursive

依然范特西╮ 提交于 2019-12-05 08:43:19

To be honest, recursive locking does have some uses but generally it's a hack. I can't seem to find the article right now, but Butenhof has a nice rant on this.

Back to the question. You could keep a thread-specific flag that signals: "I have the lock". Set it right after locking and unset it before unlocking. Since this is the only thread accessing it, you should be safe. So when trying to lock you simply need to check: "Hey, is this thing locked already?".

As a side note: are you sure the design is okay if a thread tries to lock twice ?

EDIT

Found the article.

But if that's all that's necessary, why does POSIX have recursive mutexes?

Because of a dare.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!