gcc 和可重入代码
GCC 是否为所有场景生成可重入代码?
Does GCC generate reentrant code for all scenarios ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
GCC 是否为所有场景生成可重入代码?
Does GCC generate reentrant code for all scenarios ?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
不,您必须编写可重入代码。
no, you must write reentrant code.
ISO C 和 C++ 在设计上就具备可重入性,GCC 也包括在内。您仍然有责任编写可重入函数的代码。
即使函数针对可重入性进行了正确编码,AC 编译器也不会生成可重入代码,这只是例外而不是规则,并且是出于体系结构限制的原因(例如没有足够的资源来支持堆栈,因此生成静态帧)。在这些情况下,编译器文档应该明确说明这一点。
您可能会读到的一些文章:
Reentrancy is something that ISO C and C++ are capable of by design, so that includes GCC. It is still your responsibility to code the function for reentrancy.
A C compiler that does not generate reentrant code even when a function is coded correctly for reentrancy would be the exception rather than the rule, and would be for reasons of architectural constraint (such as having insufficient resources to support stack, so generating static frames). In these situations the compiler documentation should make this clear.
Some articles you might read:
不,GCC 不保证您编写的代码。这是编写可重入代码的一个很好的链接。
https://www.ibm.com/support/knowledgecenter/ en/ssw_aix_71/generalprogramming/writing_reentrant_thread_safe_code.html
No, GCC does not guarantee for the code written by you. Here is a good link for writing re-entrant code.
https://www.ibm.com/support/knowledgecenter/en/ssw_aix_71/generalprogramming/writing_reentrant_thread_safe_code.html
可重入性不是编译器可以控制的——由程序员编写可重入代码。为此,您需要避免所有明显的陷阱,例如全局变量(包括局部静态变量)、共享资源、线程、对其他不可重入函数的调用等。
话虽如此,一些针对小型嵌入式系统的交叉编译器,例如8051,默认情况下可能不会生成可重入代码,您可能必须通过例如
#pragma
请求特定功能的可重入代码。Re-entrancy is not something that the compiler has any control over - it's up to the programmer to write re-entrant code. To do this you need to avoid all the obvious pitfalls, e.g. globals (including local static variables), shared resources, threads, calls to other non-reentrant functions, etc.
Having said that, some cross-compilers for small embedded systems, e.g. 8051, may not generate reentrant code by default, and you may have to request reentrant code for specific functions via e.g. a
#pragma
.GCC 至少在其编译的大多数平台上生成可重入代码(特别是如果您避免按值传递或返回结构),但特定语言或平台 ABI 可能会另有规定。您需要更加具体才能做出更具结论性的陈述;我知道,如果正在编译的代码本身基本上是可重入的,那么它在桌面处理器上肯定是基本上可重入的(当然,奇怪的全局状态技巧可能会让您在任何平台上陷入麻烦)。
GCC generates reentrant code on at least the majority of platforms it compiles for (especially if you avoid passing or returning structures by value) but it is possible that a particular language or platform ABI might dictate otherwise. You'll need to be much more specific for any more conclusive statement to be made; I know it's certainly basically reentrant on desktop processors if the code being compiled is itself basically reentrant (weird global state tricks can get you into trouble on any platform, of course).
不,GCC 无法保证您编写的可重入代码。
然而,在主要平台上,编译器生成或包含的代码(例如数学内在函数或函数调用)是可重入的。由于 GCC 不支持常见不可重入函数调用的平台(例如 8051),因此编译器出现可重入问题的风险很小。
有些 GCC 端口存在错误和问题,例如 MSP430 版本。
No, GCC cannot possibly guarantee re-entrant code that you write.
However, on the major platforms, the compiler produced or included code, such as math intrinsics or function calls, are re-entrant. As GCC doesn't support platforms where non-reentrant function calls are common, such as the 8051, there is little risk in having a compiler issue with reentrancy.
There are GCC ports which have bugs and issues, such as the MSP430 version.