Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

reentrancy

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-like tools can be used to check for these issues?

like image 913
Fanatic23 Avatar asked Jul 13 '10 18:07

Fanatic23


2 Answers

  • 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).
like image 66
Ben Voigt Avatar answered Sep 22 '22 01:09

Ben Voigt


None really. Writting non-reentering code is usually more difficult than re-entring. Just follow those simple guidelines and don't try to do anything too waky and you'll be fine.

Non-reentering code is usually written for high-performance issues.

like image 38
Gianni Avatar answered Sep 21 '22 01:09

Gianni