Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   InterlockedIncrement in welcher Unit? (https://www.delphipraxis.net/216113-interlockedincrement-welcher-unit.html)

delphifan2004 30. Okt 2024 09:59

InterlockedIncrement in welcher Unit?
 
Hallo,

in welcher Unit finde ich die Routine InterkolckedIncrement? In der Windows Unit ist sie offenbar nicht.

supermulti81 30. Okt 2024 10:19

AW: InterlockedIncrement in welcher Unit?
 
Hallo,

hilft dir das weiter:
https://docwiki.embarcadero.com/Libr...s.TInterlocked

Increment ist eine Methode von TInterlocked.

Viele Grüße

Uwe Raabe 30. Okt 2024 10:40

AW: InterlockedIncrement in welcher Unit?
 
Zitat:

Zitat von delphifan2004 (Beitrag 1542728)
In der Windows Unit ist sie offenbar nicht.

Ist sie doch:
Delphi-Quellcode:
function InterlockedIncrement(var Addend: Integer): Integer; stdcall; inline;
{$EXTERNALSYM InterlockedIncrement}

delphifan2004 30. Okt 2024 12:12

AW: InterlockedIncrement in welcher Unit?
 
Danke soweit, aber dann stimmt irgendwas in meinen Projekteinstellungen nicht. Hab System.SynObjs jetzt in der Uses Klausel aber der Compiler meldet Routine immer noch als nicht gefunden.

Was könnte da noch faul sein?

Ist da eventuell die Schreibweise falsch:

so steht die Routine in meinem Projekt:

InterlockedIncrement --- mit Copy und Paste aus dem Projekt hierher kopiert!

Kann ich mir echt nicht erklären, habe doch Delphi nach Standard installiert!

Papaschlumpf73 30. Okt 2024 12:27

AW: InterlockedIncrement in welcher Unit?
 
Vielleicht könntest du uns auch mal eine ganze Zeile Code zeigen?

Uwe Raabe 30. Okt 2024 12:29

AW: InterlockedIncrement in welcher Unit?
 
Dann zeig doch mal den ganzen Code (falls zu groß als Anhang). Manchmal sieht man ja den Wald vor lauter Bäumen nicht.

Papaschlumpf73 30. Okt 2024 12:32

AW: InterlockedIncrement in welcher Unit?
 
Oder du probierst das mal ganz einfach aus:
Delphi-Quellcode:
procedure TFMain.Button1Click(Sender: TObject);
var A: Integer;
begin
A:=1;
Winapi.Windows.InterlockedIncrement(A);
showmessage(A.ToString);
end;
Da sollte dann 2 rauskommen. :-D

himitsu 30. Okt 2024 12:36

AW: InterlockedIncrement in welcher Unit?
 
Verwende stattdessen das "neue" platformunabhänige Delphi-Referenz durchsuchenAtomicIncrement (aus der System-Unit)

Aber wenn man das InterlockedIncrement richtig schreibt, dann lässt es sich auch in der Windows-Unit (Winapi.Windows) finden. :stupid:

delphifan2004 30. Okt 2024 12:43

AW: InterlockedIncrement in welcher Unit?
 
Das hieße ja dass ich die Klasse TInterlocked nicht erst instanziieren muss?

Ich habe ein Internet Beispielprogramm das mir einen eigenen Speichermanager bereit stellt, ich habe mir daraus die folgende Unit gebaut:

Delphi-Quellcode:
unit mem_mgr;

interface
uses Windows, System.SyncObjs;

var
  OldMemMgr      : TMemoryManagerEx;
  GetMemCalls    : Integer;
  FreeMemCalls   : Integer;
  MaxAvail       : Integer;
  ReallocMemCalls : Integer;
  AllocMemCalls  : Integer;

function MyGetMem(Size: Integer): Pointer;
function MyFreeMem(P: Pointer): Integer;
function MyReallocMem(P: Pointer; Size: Integer): Pointer;
function MyAllocMem(Size: Cardinal): Pointer;


implementation

//uses System.SyncObjs;

function MyGetMem(Size: Integer): Pointer;
begin
  { Route the call. }
  Result := OldMemMgr.GetMem(Size);

  { Safely increment the counter. }
  winapi.windows.InterlockedIncrement(GetMemCalls); //Bereits hier meckert der Compiler
end;

function MyFreeMem(P: Pointer): Integer;
begin
  { Route the call. }
  Result := OldMemMgr.FreeMem(P);

  { Safely increment the counter. }
  Interlocke1dIncrement(FreeMemCalls);
end;

function MyReallocMem(P: Pointer; Size: Integer): Pointer;
begin
  { Route the call. }
  Result := OldMemMgr.ReallocMem(P, Size);

  { Safely increment the counter. }
  InterlockedIncrement(ReallocMemCalls);
end;

function MyAllocMem(Size: Cardinal): Pointer;
begin
  { Route the call. }
  Result := OldMemMgr.AllocMem(Size);

  { Safely increment the counter. }
  InterlockedIncrement(AllocMemCalls);
end;

(*
procedure TForm2.btPerformSomethingClick(Sender: TObject);
var
  X : Integer;
  I : ^Integer;
begin
  { Perform some allocations and free operations. }
  for X := 0 to 999 do
  begin
    New(I);
    Dispose(I);
  end;
end;

procedure TForm2.btUseMyMemMgrClick(Sender: TObject);
var
  MyMemMgr : TMemoryManagerEx;
begin
  { Switch button states. }
  btUseSystemMemMgr.Enabled := true;
  btUseMyMemMgr.Enabled := false;

  { Get the old memory manager. }
  GetMemoryManager(OldMemMgr);

  { Create your instance. }
  MyMemMgr.GetMem := MyGetMem;
  MyMemMgr.FreeMem := MyFreeMem;
  MyMemMgr.ReallocMem := MyReallocMem;
  MyMemMgr.AllocMem := MyAllocMem;

  { Use the defaults for this--not important. }
  MyMemMgr.RegisterExpectedMemoryLeak := OldMemMgr.RegisterExpectedMemoryLeak;
  MyMemMgr.UnregisterExpectedMemoryLeak := OldMemMgr.UnregisterExpectedMemoryLeak;

  { Clear out the count variables. }
  GetMemCalls := 0;
  FreeMemCalls := 0;
  ReallocMemCalls := 0;
  AllocMemCalls := 0;

  { Install the new memory manager. }
  SetMemoryManager(MyMemMgr);
end;
*)
end.
Ich dachte zuerst man müsse eine Klasse vom Typ TInterlocked instanziieren, um Interlocked verwenden zu können wie oben ein User suggereiert hat der mich auf die DocWiki verwiesen hat. Demnach brauche ich ja die Klsse TInterlocked, die aber in diesem Internetbeispiel nicht verwendet wird.

Wie aufwendig ist eigentlich der Bau einer eigenen Interlocked Routine um mein Problem zu umgehen?

Papaschlumpf73 30. Okt 2024 12:49

AW: InterlockedIncrement in welcher Unit?
 
Und was ist das?
Delphi-Quellcode:
function MyFreeMem(P: Pointer): Integer;
begin
  { Route the call. }
  Result := OldMemMgr.FreeMem(P);

  { Safely increment the counter. }
  Interlocke1dIncrement(FreeMemCalls);
end;
Interlocke1???


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:13 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz