English 中文(简体)
具备“深层”参数(现成)安全
原标题:Are `const` string parameters (thread) safe

本条

procedure MyThreadTestA(const AStr: string);

Is faster than

procedure MyThreadTestB(AStr: string);

在从事同样工作的同时,双方都走了一条路。

However version B correctly updates the referencecount of AStr and makes a copy if I change it.
Version A passes just a pointer and only the compiler prevents me from changing AStr.

Version 如果我在议会中玩s,或者为了绕过编纂者的保护,那是不安全的。

Is passed AStr by reference as a const parameters thread safe?
What happens if AStr s reference count in some other thread goes to zero and the string is destroyed?

最佳回答

无,这种骗子不是read。 <代码>Const 防止添加-ref,这样,另一条面的改动就会以不可预测的方式影响其价值。 抽样方案,在<代码>定义中尝试修改<代码>const P :

{$apptype console}
uses SysUtils, Classes, SyncObjs;

type
  TObj = class
  public
    S: string;
  end;

  TWorker = class(TThread)
  public
    procedure Execute; override;
  end;

var
  lock: TCriticalSection;
  obj: TObj;

procedure P(const x: string);
// procedure P(x: string);
begin
  Writeln( P(1): x =  , x);
  Writeln( Releasing obj );
  lock.Release;
  Sleep(10); // give worker a chance to run
  Writeln( P(2): x =  , x);
end;

procedure TWorker.Execute;
begin
  // wait until TMonitor is freed up
  Writeln( Worker started... );
  lock.Acquire;
  Writeln( worker fiddling with obj.S );
  obj.S :=  bar ;
  TMonitor.Exit(obj);
end;

procedure Go;
begin
  lock := TCriticalSection.Create;
  obj := TObj.Create;
  obj.S :=  foo ;
  UniqueString(obj.S);
  lock.Acquire;
  TWorker.Create(False);
  Sleep(10); // give worker a chance to run and block
  P(obj.S);
end;

begin
  Go;
end.

但是,它不仅局限于深层;修改基本变形地点具有类似影响:

{$apptype console}
uses SysUtils, Classes, SyncObjs;

type
  TObj = class
  public
    S: string;
  end;

var
  obj: TObj;

procedure P(const x: string);
begin
  Writeln( P(1): x =  , x);
  obj.S :=  bar ; 
  Writeln( P(2): x =  , x);
end;

procedure Go;
begin
  obj := TObj.Create;
  obj.S :=  foo ;
  UniqueString(obj.S);
  P(obj.S);
end;

begin
  Go;
end.
问题回答

在Barry的答复中添加: 如果从电离器范围内的一个地方变量中穿透镜,那肯定是read。

In that case that local variable will hold a valid reference and the only way (assuming just valid pascal code, no fiddling around in asm) for that local variable to be changed is if your call returns.

这还包括由职能电话(包括财产存取,例如TStrings)产生的星座变数来源的所有情况。 优点在于,在这种情况下,汇编者必须在当地的温变中储存插座。

只有当你直接从一个地点(在同一或另一处)经人叫回之前可以改变这种扼杀,才可能产生表面安全问题。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签