如何在源代码中强制执行单线程构建 [英] How to enforce single threaded build in source code

查看:66
本文介绍了如何在源代码中强制执行单线程构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我为一些非常专业的数据处理创建了许多小型实用程序.通常,我是唯一的用户.我什至不考虑多线程编程,因为运行时性能对于我的用例来说已经足够了.关键资源是我的编程时间.所以我想避免多线程编程所需的任何额外工作.

Background: I create many small utilities for some very specialised data processing. Often, I am the only user. I don't even think about multi-threaded programming, because the run-time performance is by far sufficient for my use cases. The critical resource is my programming time. So I want to avoid any extra effort required for multi-threaded programming.

但是,当我将来重用我的代码时,我的源代码在多线程上下文中执行似乎存在风险.

However, it seems there is a risk, that my source code gets executed in a multi-threaded context, when I reuse my code in future.

根据CppCoreGuidelines:

请注意:有许多示例中已知"的代码never run in a multi-threaded program was run as part of a multi-threaded program多线程程序.很多年后.通常,此类程序导致消除数据竞争的痛苦努力.因此,代码是从来没有打算在多线程环境中运行应该是清楚地标记为这样,最好带有编译或运行时尽早发现这些使用错误的强制机制.

Be careful: there are many examples where code that was "known" to never run in a multi-threaded program was run as part of a multi-threaded program. Often years later. Typically, such programs lead to a painful effort to remove data races. Therefore, code that is never intended to run in a multi-threaded environment should be clearly labeled as such and ideally come with compile or run-time enforcement mechanisms to catch those usage bugs early.

同一来源中的大多数建议实际上让我开始了多线程编程.我更愿意遵循的一个建议是:

Most of the suggestions in the same source actually get me started with multi-threaded programming. The one suggestion, which I prefer to follow says:

拒绝在多线程环境中构建和/或运行.

Refuse to build and/or run in a multi-threaded environment.

所以我的问题是,我该怎么做?例如.是否有包含文件,#pragma 左右,以确保单线程构建/执行源文件中的所有内容?

So my question is, how do I do that? E.g. is there an include file, #pragma or so to ensure single threaded build / execution of everything within a source file?

推荐答案

With g++/gcc 编译和链接多线程代码需要使用 -pthread 编译器和链接器选项.此选项设置 _REENTRANT 宏,您可以在编译时检查它:

With g++/gcc compiling and linking multithreaded code requires using -pthread compiler and linker option. This option sets _REENTRANT macro which you can inspect at compile time:

$ g++ -E -dD -xc++ /dev/null > a
$ g++ -pthread -E -dD -xc++ /dev/null > b
$ diff a b
289a290
> #define _REENTRANT 1

与流行的看法相反,使用 -lpthread 链接器选项是不必要的,也不足以正确构建多线程程序.

Contrary to popular belief, using -lpthread linker option is unnecessary and insufficient to correctly build a multi-threaded program.

Microsoft Visual Studio 为多线程构建 IIRC 设置了 _MT 宏.

Microsoft Visual Studio sets _MT macro for a multi-threaded build, IIRC.

Boost 库执行以下操作:

Boost library does the following:

// Turn on threading support if the compiler thinks that it's in
// multithreaded mode.  We put this here because there are only a
// limited number of macros that identify this (if there's any missing
// from here then add to the appropriate compiler section):
//
#if (defined(__MT__) || defined(_MT) || defined(_REENTRANT) \
    || defined(_PTHREADS) || defined(__APPLE__) || defined(__DragonFly__)) \
    && !defined(BOOST_HAS_THREADS)
#  define BOOST_HAS_THREADS
#endif

这样您就可以#include 然后检查BOOST_HAS_THREADS 宏的值.

So that you can #include <boost/config.hpp> and then inspect the value of BOOST_HAS_THREADS macro.

如果以多线程模式构建,以下会导致编译错误:

The following causes a compilation error if it is built in multi-threaded mode:

#if defined(BOOST_HAS_THREADS) || defined(_REENTRANT) || defined(_MT)
#error This code is single-threaded only.
#endif

这篇关于如何在源代码中强制执行单线程构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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