重入函数???????? [英] re-entrant function????????

查看:74
本文介绍了重入函数????????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




C大师中的任何一个人都可以在那里找出任何一个

re-entrnt函数的例子,只是为了说明什么是重要的那个

属性以及我们如何利用它....


sushant

解决方案

th********@rediffmail.com 写道:< blockquote class =post_quotes>

C大师的任何一个人都可以在那里建议任何一个
重新调用函数的例子,只是为了表明它的意义是什么
属性以及我们如何利用它....

sushant




在使用/编写多线程代码时会成为一个问题。如果

函数依赖于静态数据,并且在另一个线程已经调用并且正在执行的情况下被调用,则该静态数据可能会被更改为

不同的线程并产生意想不到的结果


th *** *****@rediffmail.com 写道:


任何C大师选址那里建议任何一个
重新调用函数的例子只是为了显示该
属性的重要性以及我们如何利用它....




#include< stdio.h>


int putword(unsigned long w,FILE * f)

{

if(w> 9)

if(0> putword(w / 10,f))返回EOF;

返回putc((w%10)+''0'',f);

} / * putword * /


/ * --------------- * /


#ifdef测试


#include< stdlib.h>


int main(void)
{

int i;


for(i = 0;我< 10; i ++){

putword(i,stdout);

putc('''',stdout);

putword(rand(), stdout);

putc(''\ n'',stdout);

}

返回0;

} / * main * /

#endif

-

查克F(cb ******** @ yahoo .com)(cb********@worldnet.att.net)

适用于咨询/临时嵌入式和系统。

< http:/ /cbfalconer.home.att.net>使用worldnet地址!


Up spake th ** ******@rediffmail.com

C大师的任何一个人都可以在那里找出任何一个
重新调用函数的例子,只是为了显示什么是这个
属性的重要性以及我们如何利用它....




这是可重入的


int

f(int x)

{

int y;


y = x * x ;

返回y;

}


这不是


int y ;


int

f(int x)

{

y = x * x;

返回y;

}


可重入函数与非可重入函数的另一个例子是strtok_r

vs. strtok。


正如OP所说,当你在一个进程中拥有多个线程

(轻量级进程)时会出现问题,因为(某些)州是

在多个执行线程之间共享。


规范的例子是


int x;

void f (void){x ++;两个执行f()的线程的
。 ++操作(通常)将

解析为三个汇编指令:copy-memory-to-register,


例如,考虑第一个线程执行

前两个指令的情况,然后第二个线程执行所有三个指令,然后

第一个线程执行最后一条指令。第二个线程'

更改将丢失,并且该地址的内存内容将为

不正确。


这类错误是一个可以找到的PITA,因为症状是间歇性的,因此难以重现。


-

-trent

乔怎么想,一个人没有信仰,没有信仰就能有勇气吗?

Burroughs一无所获,但在那里他像Luther一样固执。 br />


hi,

can anyone of C masters siting out there suggest any 1 example of
re-entrnt function just to show what is the significance of that
property and how we can exploit it ....

sushant

解决方案

th********@rediffmail.com wrote:

hi,

can anyone of C masters siting out there suggest any 1 example of
re-entrnt function just to show what is the significance of that
property and how we can exploit it ....

sushant



that becomes an issue when using/writing multi-threading code. If a
function relies on static data, and is called while another thread has
already called it and is executing, that static data may be changed by
different threads and produce unexpected results


th********@rediffmail.com wrote:


can anyone of C masters siting out there suggest any 1 example of
re-entrnt function just to show what is the significance of that
property and how we can exploit it ....



#include <stdio.h>

int putword(unsigned long w, FILE *f)
{
if (w > 9)
if (0 > putword(w / 10, f)) return EOF;
return putc((w % 10) + ''0'', f);
} /* putword */

/* --------------- */

#ifdef TESTING

#include <stdlib.h>

int main(void)
{
int i;

for (i = 0; i < 10; i++) {
putword(i, stdout);
putc('' '', stdout);
putword(rand(), stdout);
putc(''\n'', stdout);
}
return 0;
} /* main */
#endif
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Up spake th********@rediffmail.com:

can anyone of C masters siting out there suggest any 1 example of
re-entrnt function just to show what is the significance of that
property and how we can exploit it ....



This is re-entrant

int
f (int x)
{
int y;

y = x * x;
return y;
}

This is not

int y;

int
f (int x)
{
y = x * x;
return y;
}

Another example of re-entrant vs. non-reentrant functions is strtok_r
vs. strtok.

As the OP said, it becomes an issue when you have multiple threads
(lightweight processes) in a single process, because (some) state is
shared between multiple threads of execution.

The canonical example is

int x;
void f (void) { x++; }

with two threads executing f(). The ++ operation (typically) resolves
into three assembly instructions: copy-memory-to-register,
increment-register, and copy-register-to-memory.

Consider, for example, the situation where first thread executes the
first two instructions, then the second thread executes all three, then
the first thread executes the last instruction. The second thread''s
change will be lost, and the contents of memory at that address will be
incorrect.

This class of bugs are a PITA to locate, because symptoms are
intermittent and therefore difficult to reproduce.

--
-trent
How, Joe wondered, can a man have courage without faith, without belief?
Burroughs believed in nothing, and yet there he sat stubborn as Luther.


这篇关于重入函数????????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆