简体   繁体   English

使用函数指针和静态变量时是否需要readlock?

[英]Do we need readlock while using function pointer & static variable?

我们是否需要一个读锁来读取除变量小于memalign之外的整数以及某些缓存情况?

The analyser assumes that because the address of f1() has been taken, it could be called through that function pointer from any context. 分析器假设因为已经采用了f1()的地址,所以可以通过来自任何上下文的函数指针调用它。 If, for example, you have one thread updating x : 例如,如果您有一个线程更新x

lock xlock;
update x;
unlock xlock;

and another thread calls f1() simultaneously through the function pointer, the access to f1() in that second thread won't be protected by the lock. 另一个线程通过函数指针同时调用f1() ,第二个线程中对f1()的访问将不受锁的保护。 It could therefore see a partial update of x , or see the update incorrectly ordered with respect to other updates. 因此,它可以看到x的部分更新,或者查看与其他更新相关的更新错误。

The reason why the function pointer matters is that if the address of f1() is never taken, then the analyser can determine exactly where the function is called from. 函数指针很重要的原因是如果永远不会采用f1()的地址,那么分析器就可以准确地确定函数的调用位置。 When the address has been taken, it has to assume the worst case. 当地址被采用时,它必须假设最坏的情况。

From the text you quoted (emphasis mine): 从你引用的文字(强调我的):

If a function has had its address taken, then we presume that we do not know the context of every call made upon that function . 如果一个函数已经获取了它的地址,那么我们假设我们不知道对该函数进行的每个调用的上下文

Because lint cannot determine every context that a call to f1 could be made in, it assumes that it could be called in any context, including those in which accessing x results in some data race. 因为lint无法确定可以调用f1每个上下文,所以它假定它可以在任何上下文中调用,包括访问x导致某些数据竞争的上下文。

Reasonably, you probably won't hit anything dire by returning x . 合理地说,你可能不会通过返回x击中任何可怕的东西。 But it's a probability, and the whole point of using lint is to reduce uncertainty with respect to how your code functions with unexpected inputs. 但这是一个概率,使用lint的重点是减少关于代码如何与意外输入一起工作的不确定性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM