预处理程序宏禁止使用静态变量,但允许使用静态函数 [英] preprocessor macro to disallow static variable but allow static functions

查看:118
本文介绍了预处理程序宏禁止使用静态变量,但允许使用静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要确保我编写了线程安全的代码并检查了十几个项目中代码的线程安全性,我想在大多数源文件中禁止使用静态变量.但是,如果两个文件具有相同名称的函数,我可能仍希望能够使用静态函数.

禁止使用
完成静态使用
#定义静态STATIC_DATA_IS_NOT_THREAD_SAFE

但是,我不确定如何允许静态函数...

您能建议一个替代方法吗?
我能想到的是编写一个程序以读取我的所有代码,并记录所有出现静态但没有打开括号的文件.
或者,我可以完全禁止使用静态函数,并根据源文件名将静态函数包装在名称空间中.

To make sure that I write thread-safe code and to check the thread safety of my code in more than a dozen projects, I would like to disallow use of static variables in most of my source files. However, I might still like to be able to use static functions in case two files have functions with the same name.

Disallowing static use as accomplished using

#define static STATIC_DATA_IS_NOT_THREAD_SAFE

However, I''m not sure how to allow static functions...

Could you suggest an alternative?
The one I can think of is writing a program to read all of my code and logging any files where static appears without an open parenthesis.
Alternatively, I can forbid static altogether and wrap my static functions in namespaces based on the source file name.

推荐答案

将问题转回原处...

为什么首先要使用文件/模块级别的静态函数和数据?如果需要文件/模块级的功能和数据,请使用匿名名称空间.

删除静态变量不会使您的代码线程安全顺便说一句-除非您非常小心,否则任何形式的共享数据或变量别名都可能使您陷入困境.

干杯,



PS回应以下评论...

如果仅使用命名空间来限制对同一文件中其他对象的访问,则无需命名它们.如果您这样做:

Turning the question back around...

Why are you using file/module level static functions and data in the first place? If you want file/module level functions and data then use an anonymous namespace.

Removing statics won''t make your code thread safe BTW - any form of shared data or aliasing of variables can sink you unless you''re very careful.

Cheers,

Ash

PS in response to the comment below...

You don''t need to name your namepaces if you''re just using them to restrict access to other objects in the same file. If you do:

namespace
{
    bool do_something_groovy_that_was_static()
    {
        //...
    }
}



那么只有与do_something_groovy_that_was_static在同一文件中的内容才能调用它.

就多线程而言,唯一绝对可以安全使用的代码是不使用未在该代码中直接创建的任何东西的代码.因此,仅局部变量且仅使用没有副作用的函数.这些天,您可以使用多线程分配器来进行内存分配,但是我不会再犹豫了.

全局变量是所有多线程弊端的根源之一-确保您使用的所有全局资源都正确同步.如果需要,可以将它们包装在课堂上,但要确保没有获得某种形式的锁就不会对他们产生任何伤害.无论是在函数,类还是在文件中,静态变量也是如此.

一个建议是,如果您使用相当严格的测试驱动开发(作为副作用,它会删除诸如globals和statics之类的东西,因为几乎不可能为依赖于这些东西的代码编写单元测试),那么您的代码将是安全得多.从我有限的经验来看,这种方法似乎行得通,因此将来可能会遵循该方法.



then only things in the same file as do_something_groovy_that_was_static will be able to call it.

As far as multithreading the only code that''s absolutely safe to use is something that doesn''t use anything not directly created in that code. So local variables only and only use functions with no side effects. These days you can get away with memory allocation with multithreaded allocators but I wouldn''t stick my neck out much further.

Globals are one of the roots of all mulithreading evils - make sure that any global resources you use are synchronised properly. Wrap them up in a class if you have to but make sure nothing can get at them without acquiring a lock of some sort. The same goes for statics as well, whether in functions, classes or files.

One suggestion is that if you use fairly strict test driven development (which, as a side effect, removes things like globals and statics as it''s almost impossible to write unit tests for code that depends on these things) then your code will be a lot more thread safe. From my limited experience that seems to work so it might be a route to follow in future.


这篇关于预处理程序宏禁止使用静态变量,但允许使用静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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