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

问题描述

void reverse_string(char* string, int str_size) {
    char tmp;
    int i = 0;
    int j = str_size - 1;
    while (i < j) {
        tmp = string[i];
        string[i] = string[j];
        string[j] = tmp;
        ++i;
        --j;
    }
}

我认为这个函数是可重入的,因为它不使用任何全局变量.它只会修改参数.

I think this function is reentrant, since it doesn't use any global variable. It only modifies the arguments.

我的问题是:该函数可重入吗?如果可以,我的论点够吗?

My question is:is this function reentrant?if it is, is my argument good enough?

预先感谢

推荐答案

是的,这是一个可重入函数.可重入函数定义为在它们自身执行时(由于递归或并发性)可以调用的函数.在这种情况下,递归是没有意义的,并且您同时安全(假设参数不同).

Yes, this is a reentrant function. Reentrant functions are defined as those that can be called whilst they are themselves executing (either due to recursion, or concurrency). In this case, recursion is moot, and you are concurrently safe (assuming differing parameters).

您的参数很好-没有显式或隐式访问全局或共享状态,因此可确保重入.这是您的显式代码和C语义的结合.其他语言和API 可能不具有此属性.

Your argument is fine - there's no global or shared state being accessed either explicitly or implicitly, so reentrancy is ensured. This is a combination of both your explicit code and the semantics of C. Other languages and APIs may not have this property.

仔细检查后,ISO C标准似乎并未强制strlen的线程安全.这样,您极有可能使用带有非线程安全strlen的C标准库,并因此从中继承非可重入.

On double checking, the ISO C standard doesn't seem to force the thread safety of strlen. As such, there is a tiny possibility that you could be using a C standard library with a non-thread safe strlen and as such, inherit non-reentrancy from it.

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

09-05 11:45
查看更多