当前位置:网站首页>shared usage in d
shared usage in d
2022-08-09 20:16:00 【fqbqrr】
原文
永远不要使用__gshared
.This is a clear security hole.改为使用shared
.
If encountered when usingshared
的编译
错误,那是编译器
warning you.Should be serious考虑
线程安全,Then just drop it in the right placeshared
.
使用__gshared
,The compiler pretends not to see it变量
是共享
的.And unless you think carefully,Guaranteed to generate竞争
.
顺便,有优先__gshared
而非shared
的情况吗?It seems that many newbies are using it__gshared
.
我不完全
Agree with that,It just depends on your usage.一般,你应该使用shared
,但__gshared
有意义.只是在多线程
It only has a problem if it can be changed,But if it's only from单线程
Change but from多线程
读取,is generally not a problem.
总之:
单写/单读?用__gshared
单写/多读?用__gshared
多写/单读?用shared
多写/多读?用shared
如果与C对接,则需要__gshared
.但是,是的,这里应该使用shared
.
__gshared
表现不错,But I use it firststd.concurrency
,示例:
import std.stdio;
import std.concurrency;
import core.thread;
struct Result {
int value;
}
struct Done {
}
void run()
{
bool done = false;
while (!done) {
writeln("运行子线程");
receiveTimeout(1.seconds,
(Done msg) {
done = true;
});
}
// `发送`Result to owner
// 假定,Threads produce results in the above loop
ownerTid.send(Result(42));
}
void main()
{
auto worker = spawn(&run);
Thread.sleep(5.seconds);
worker.send(Done());
auto result = receiveOnly!Result();
writeln("结果:", result);
}
所有这些
都可能是竞争条件
.
这是Write and read
的:
align(64) static struct S
{
align(1):
ubyte[60] off;
ulong x = 0;
}
__gshared S s;
void main()
{
import core.thread: Thread;
import std.conv: to;
new Thread(() {
foreach (i; 0 .. uint.max)
{
s.x = 0;
s.x = -1;
}
}).start();
foreach (i; 0 .. uint.max)
{
auto x = s.x;
assert(x == 0 || x == -1, to!string(x, 16));
}
}
If you know how安全
地访问变量
,则可用shared
.我坚持:永远不要使用__gshared
.
A quick test showsextern(C) extern shared
工作很好.
据我所知,__gshared
仅在,你想在单线程
程序中访问共享C变量
Works well.然后,if used later多线程
,你仍然会失败
.
所以,永远不要(在多线程
代码中)使用__gshared
.
C没有共享
概念,So not correct类型
.加上shared
只是假
,and cause trouble.最好明确
说明.
Not that you should use it freely__gshared
,或只在D中
使用.而是说Never should
使用它,这是错误的.
废话.Add to the shared variableshared
不是"说谎".C
It doesn't matter if you differentiate.但D
重要.
if you can identify it__gshared
的有效用例
,并用它编写
正确代码,then you will know什么
don't listen to me.
其他人,永远
不要使用__gshared
.__gshared
和-boundscheck=off
一样糟糕.They are all obvious安全漏洞
.
auto x=s.x;
Here is your problem,not because it is__gshared
.
You copied the value,Obviously it can be changed at the same time,这是常识.
You should not use it this way.Instead, it should be accessed directlys.x
.
而用共享
,如果读取线程
lock first,则结果相同,且在更改前
The value is read and processed.
读x
时,就改了x
.
auto x = s.x;
assert(s.x == 0 || s.x == -1, to!string(s.x, 16));
//Multiple competing alternatives to the original1a competition,and cannot locate the problem
shared
It doesn't solve the conditional race,这是对的.如果没有-preview=nosharedaccess
,There is no difference.所以不妨使用shared
.
但是有了-preview=nosharedaccess
,The code no longer compiles,你不得不
考虑如何安全
访问共享数据
.哪个好?
所以:永远不要使用__gshared
,总是用-preview=nosharedaccess
.
如果你有更好
Abstract and careful锁定
访问数据,但C不行
,如果想访问C全局变量
,应使用__gshared
,Because that's what it is用途
.使用shared
,帮不了你.
使用__gshared
来共享数据给C
,与使用-boundscheck=on
Send arrays to no such restrictionsC中
一样安全.
这里结论
Really should be,不要使用C
.
不,使用shared
can really help you.
C没有shared
限定符,但C程序员
still have to be considered线程安全
.调用C函数
或访问C全局变量
必须考虑多线程
.shared加上(-preview=nosharedaccess)
force you to think合同
是什么.__gshared
没有.
不,这不对.C
总是不安全
的,这是正确的,但不重要.The point is that you are thereD端
可以/不能做什么
.
-boundscheck=on
,not easilyD端
搞砸.C
The end can still be messy.-boundscheck=off
,很容易
在Dend fucked.shared
,not easilyDend fucked.C
The end can still be messy.__gshared
,很容易在D端
搞砸.
shared
让你感觉该语言
Help you prevent problems.同样,对C,这是假的
.
边界
在C和D中的定义相同,有指针
和大小
,You cannot exceed that size.是的,Data is communicated in different ways,But it's easy to understand and use.shared
(加上-preview=nosharedaccess
),阻止
You play.No foul.Can't hurt自己
.可通过强制转换
告诉编译器(1)
Are you sure you want to play,及(2)
,你将按CPlay with the rules of the end.__gshared
Just keep you in球场
上奔跑.I don't know the rules?The compiler doesn't care.玩得开心
Break your leg.
用__gshared
:
extern(C) extern __gshared int x;
void fun() {
x = 42; }
//编译,条件竞争
我甚至
Didn't notice I was doing it危险
事情,因为第一次天真
which compiles and seems to pass工作正常
.
使用shared
(加-preview=nosharedaccess
):
extern(C) extern shared int x;
void fun() {
x = 42; } /* 错误 */
If you look at the documentation,会发现关于正确用法
.正如你所建议的,我想出了:
extern(C) extern shared int x;
void fun() {
properlyUse(&x, 42); }
//仍错误,但共享.
I am forced更多
consider线程安全
.It can be discarded hereshared
,Because the call is thread-safeproperlyUse
函数.所以:
extern(C) extern shared int x;
void fun() {
properlyUse(cast(int*) &x, 42); }
//编译,正确.
用__gshared
This is unlikely to happen.没有转换
的调用properlyUse
可能更好,But I don't trust people记得
在没有编译器
对他们大喊大叫
use this function.
Even if they do it right the first time,随着时间
推移,They will also fail.当简单的,错误的
代码编译时,It will definitely go in源文件
.
It's hard to get it right线程安全
.需要从编译器
All help obtained.__gshared
Provides zero help.shared
至少高亮
interesting place.
边栏推荐
猜你喜欢
随机推荐
毕昇编译器优化:Lazy Code Motion
International Soil Modeling Consortium-ISMC
.NET 6 study notes (4) - Solve the Nullable warning in VS2022
动态RDLC报表(三)
EPIC是什么平台?
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
【代码审计】——PHP项目类RCE及文件包含下载删除
秋招面试大厂总被刷下来,你这样做保准你事半功倍!
【工业数字化大讲堂 第二十期】制造业数字化能力建设分享,特邀制造业高级咨询顾问 李东老师分享
C#介绍及基本数据类型
Cortex-A7 MPCore Architecture
win10 uwp 简单MasterDetail
mysql generates random name, mobile number, date
国能准能集团研发矿山数字孪生系统 填补国内采矿行业空白
Logic unauthorized and horizontal and vertical unauthorized payment tampering, verification code bypass, interface
国际土壤模型协会 International Soil Modeling Consortium-ISMC
Cortex-A7 MPCore 架构
50道Redis面试题,来看看你会多少?
【ROS2原理9】 QoS - 截止日期、活跃度和寿命
动态RDLC报表(五)