本文介绍了可重入代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在修改一些遗留代码,以使函数可重入。我使用 大量的全局数据(大型嵌套结构)来快速传递数据。 我的每个人的全局结构都太大了(大小) /> 用于维护副本。 关于编写我的 库的可重入版本的最佳方法的任何建议? - 请记住,我的全球变量主要是指向 *巨大的*结构。 解决方案 你的问题是偏离主题的,因为C没有说任何关于多元/ b $ b线程的信息。 < off-topic> 这在很大程度上取决于您尝试制作的功能 - 参赛者。一般的方法是使用某种互斥量来防止全局数据在同一个地方的两个地方被改变 将结果保持在一个不一致的状态。 br /> < just-curious> 你说你的代码使用了很多全局变量。我认为这是一个坏主意 ,可能你应该进行设计审查。 < / just-curious> 一个这里要提到的是互斥锁使用合作锁定。对于详细说明,互斥是一扇门,只有一个人可以一次进入并且线程排在队列中。如果线程是 维护队列,一切都很棒,但门不会停止 你跳出窗外。 不是要重新运行的运行代码。共享数据访问 将被同步。在 修改之前锁定互斥锁,并在完成后将其解锁。根据您的 问题,您甚至可能必须使用条件变量。 < / off-topic> 6月17日上午11:33,rahul< rahulsin ... @ gmail.comwrote: 你的问题是偏离主题的,因为C没有说任何关于$ 线程的内容。 Re-entrancy不需要对线程或线程安全做任何事情, 但确实ISO C没有提到诸如''reentrancy'',''threads'' 等等。 Bit Byte,如果你正在为windows编程,试试windows 新闻组。如果您正在为unix编程,请尝试comp.unix.programmer。 < snip rahul'的非主题回复> [...] 确实如此,但只是短暂的。 C99 7.1.4p4: 标准库中的函数不是保证是 可重入并且可以修改具有静态存储持续时间的对象。 脚注: 因此,a信号处理程序通常不能调用标准库 函数。 还有一个reentrancy索引条目,指向5.1。 2.3 (计划ex执行)和5.2.3(发出中断信号)以及 7.1.4。 - Keith Thompson (The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> 诺基亚 我们必须做点什么。这是事情。因此,我们必须这样做。 - Antony Jay和Jonathan Lynn,是部长 I am modifying some legacy code, to make the functions reentrant. I uselots of global data (large nested structs) to pass data around quicky.The global structures are far too large (in size) for each of myfunctions to maintain copies.Any suggestions on the best way to write a reentrant version of mylibrary? - bearing in mind that my global vars are mostly, pointers to*HUGE* structures. 解决方案Your question is off-topic as C does not say anything about multi-threading.<off-topic>That greatly depends on the function you are trying to make re-entrant. A general approach is to use some sort of mutex to preventthe global data from being altered in two place in the same placesleaving the result in an inconsistent state.<just-curious>You say your code uses a lot of globals. I think that''s a bad ideaand may be you should do a design review.</just-curious>One thing to mention here is mutex uses co-operative locking. Toelaborate, mutex is a door through which only one person can enter ata time and threads are standing in a queue. If the threads aremaintaining the queue, everything is great but the door does not stopyou from jumping out of the window.Its not the running code that is to be made re-entrant. The access tothe shared data is to be synchronized. Lock the mutex beforemodification and unlock it when you are done. Depending on yourproblem, you may even have to use condition variables.</off-topic>Your question is off-topic as C does not say anything about multi-threading.Re-entrancy doesn''t have to do anything with threads or thread safety,but indeed ISO C doesn''t mention thing such as ''reentrancy'', ''threads''etc."Bit Byte", if you are programming for windows, try a windowsnewsgroup. If you are programming for unix, try comp.unix.programmer.<snip rahul''s off-topic reply>[...]It does, but only briefly.C99 7.1.4p4:The functions in the standard library are not guaranteed to bereentrant and may modify objects with static storage duration.footnote:Thus, a signal handler cannot, in general, call standard libraryfunctions.There''s also an index entry for "reentrancy", pointing to 5.1.2.3(Program execution) and 5.2.3 (Signals an interrupts) as well as7.1.4.--Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>Nokia"We must do something. This is something. Therefore, we must do this."-- Antony Jay and Jonathan Lynn, "Yes Minister" 这篇关于可重入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 22:29