17.11 Static constructors

本文介绍了静态构造器的概念及其在初始化类中的作用。静态构造器在类的实例被创建或其静态成员被引用时触发,并且仅执行一次。文章通过示例展示了如何使用静态构造器来初始化类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A static constructor is a member that implements the actions required to
initialize a class. Static constructors are
declared using static-constructor-declarations:
static-constructor-declaration:
attributesopt static-constructor-modifiers identifier ( )
static-constructor-body
static-constructor-modifiers:
externopt static
static externopt
static-constructor-body:
block
;
A static-constructor-declaration may include a set of attributes (§24) and
an extern modifier (§17.5.7).
Chapter 17 Classes
263
The identifier of a static-constructor-declaration must name the class in
which the static constructor is declared.
If any other name is specified, a compile-time error occurs.
When a static constructor declaration includes an extern modifier, the
static constructor is said to be an external
static constructor. Because an external static constructor declaration
provides no actual implementation, its
static-constructor-body consists of a semicolon. For all other static
constructor declarations, the staticconstructor-
body consists of a block, which specifies the statements to execute in
order to initialize the class. This
corresponds exactly to the method-body of a static method with a void
return type (§17.5.8).
Static constructors are not inherited, and cannot be called directly.
The static constructor for a class executes at most once in a given
application domain. The execution of a static
constructor is triggered by the first of the following events to occur
within an application domain:
? An instance of the class is created.
? Any of the static members of the class are referenced.
If a class contains the Main method (§10.1) in which execution begins, the
static constructor for that class
executes before the Main method is called. If a class contains any static
fields with initializers, those initializers
are executed in textual order immediately prior to executing the static
constructor.
[Example: The example
using System;
class Test
{
static void Main() {
A.F();
B.F();
}
}
class A
{
static A() {
Console.WriteLine("Init A");
}
public static void F() {
Console.WriteLine("A.F");
}
}
class B
{
static B() {
Console.WriteLine("Init B");
}
public static void F() {
Console.WriteLine("B.F");
}
}
must produce the output:
Init A
A.F
Init B
B.F
because the execution of A’s static constructor is triggered by the call
to A.F, and the execution of B’s static
constructor is triggered by the call to B.F. end example]
It is possible to construct circular dependencies that allow static fields
with variable initializers to be observed in
their default value state.
[Example: The example
C# LANGUAGE SPECIFICATION
264
using System;
class A
{
public static int X;
static A() { X = B.Y + 1;}
}
class B
{
public static int Y = A.X + 1;
static B() {}
static void Main() {
Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y);
}
}
produces the output
X = 1, Y = 2
To execute the Main method, the system first runs the initializer for B.Y,
prior to class B’s static constructor.
Y’s initializer causes A’s static constructor to be run because the value
of A.X is referenced. The static constructor
of A in turn proceeds to compute the value of X, and in doing so fetches
the default value of Y, which is zero. A.X
is thus initialized to 1. The process of running A’s static field
initializers and static constructor then completes,
returning to the calculation of the initial value of Y, the result of which
becomes 2. end example]
### 如何设置DPDK 17.11环境安装配置 #### 准备工作 为了成功部署DPDK 17.11, 需要准备一台支持Intel VT-x/EPT 或 AMD-V/RVI 的物理机或虚拟化平台。操作系统应为Linux发行版,如Ubuntu、CentOS等,并确保内核版本不低于3.0[^1]。 #### 安装依赖包 在开始编译之前,先通过包管理器更新系统并安装必要的开发工具和库文件。对于基于Debian/Ubuntu系统的命令如下所示: ```bash sudo apt-get update && sudo apt-get install -y build-essential libnuma-dev dpkg-dev ``` 而对于RedHat/CentOS系列,则执行以下指令来获取所需资源: ```bash yum groupinstall "Development Tools" yum install numactl-devel ``` #### 获取源码 访问官方GitHub仓库下载指定版本的压缩包或者克隆整个项目到本地磁盘上。针对特定版本号的操作可以参照下面的例子完成: ```bash git clone http://dpdk.org/git/dpdk cd dpdk git checkout v17.11 ``` #### 编译过程 确认已切换至目标分支后,即可按照常规流程构建应用程序。在此之前建议调整部分参数以适应不同硬件条件下的性能优化需求。具体做法是在根目录下创建`.config`链接指向适合当前架构类型的预设方案之一(例如generic即通用型),之后运行make命令启动自动化组装流水线: ```bash meson setup builddir --prefix=/usr/local ninja -C builddir ``` #### 加载驱动程序 为了让网卡能够正常运作于polling模式而非中断方式处理数据收发事件,有必要加载igb_uio/uio_pci_generic模块并将选定接口绑定过去。这一步骤通常涉及卸载原有kernel自带driver再重新挂接新实例的过程,在实际操作前务必做好充分备份以防意外情况发生。 ```bash modprobe uio ./usertools/dpdk-devbind.py --status # 假定eth1是我们想要使用的网络设备名称 ifconfig eth1 down modprobe igb_uio ./usertools/dpdk-devbind.py --bind=igb_uio eth1 ``` #### 测试验证 最后可以通过简单的测试案例检验整体框架是否搭建完毕以及各项功能能否按预期发挥作用。比如利用testpmd应用模拟多路转发场景从而观察吞吐量指标变化趋势等等。 ```python ./build/app/testpmd -c 0xf -n 4 -- -i --nb-cores=2 --total-num-mbufs=8191 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值