【发布时间】:2013-11-15 18:39:46
【问题描述】:
我想使用我自己的具有一些属性的类。我可以使用read 和write 来自定义写入或读取私有变量的方式和(例如)哪个(例如)私有变量。
一个示例是此类中的整数 (MyInteger):
type
TMyClass = class
private
MyInteger : Integer;
function SomeFunction : Integer;
public
property TheInteger : Integer read SomeFunction write MyInteger;
end;
如果(例如)类会不断访问(读取)MyInteger,而另一个线程从当前实例的另一个线程访问(写入)TheInteger(写入)是否安全?
希望你们明白我的意思...基本上我不知道如果多个线程同时访问内存中的 var 是否安全...(没有关键部分)
编辑:
这个类也会有区别吗:
type
TMyClass = class
private
MyInteger : Integer;
function SomeFunction : Integer;
public
property TheInteger : Integer read MyInteger write MyInteger;
end;
还有这个:
type
TMyClass = class
private
function SomeFunction : Integer;
public
MyInteger : Integer;
end;
?
【问题讨论】:
-
不,它不是线程安全的,您需要保护共享资源免受并发访问,无论是使用临界区还是联锁 API,这是您的选择。
-
AFAIK,Delphi 中没有任何东西在外部是线程安全的。使用线程本身的东西当然应该是,但在外面,你需要始终考虑线程安全。
-
@Benjamin:这正是你想知道的。答案很明确:不,他们不是。您必须使用 CS 进行保护,就像我发布的链接所说的那样。
标签: multithreading delphi memory-management