Recommended practices for re-entrant code in C, C++

前端 未结 4 1626
半阙折子戏
半阙折子戏 2021-02-06 09:16

I was going through a re-entrancy guide on recommended practices when writing re-entrant code.

What other references and resources cover this topic?

What lint-li

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 09:24

    • Do use local variables.
    • Don't use static locals or global variables, even TLS will not help you with recursion / reentrancy.
    • Restore all your invariants before doing callbacks.
    • Don't hold locks while you do callbacks. If you absolutely must (and I would still go looking for a way to avoid it) then make sure you know what happens if you try to re-enter your lock on the thread that already holds it. At a minimum you have to test for this, otherwise depending on the lock you'll get deadlocks or broken invariants (i.e. corruption).

提交回复
热议问题