* * *

Author Topic: First time attempting multithreading  (Read 549 times)

drallim33

  • Newbie
  • Posts: 4
First time attempting multithreading
« on: June 27, 2018, 01:37:25 am »
Is this cheating? Exactly which level of hell will I be banished to for spamming these getters/setters in every class I declare?  :D

Code: Pascal  [Select]
  1. type
  2.   TMyProtectedInt = object
  3.   private
  4.     FTheInt : integer;
  5.     function GetTheInt: integer;
  6.     procedure SetTheInt(AValue: integer);
  7.   public
  8.     property Value : integer read GetTheInt write SetTheInt;
  9.   end;
  10.  
  11. var
  12.   AutoLock : TCriticalSection;
  13.  
  14. implementation
  15.  
  16. function TMyProtectedInt.GetTheInt: integer;
  17. begin
  18.   AutoLock.Acquire;
  19.   result := FTheInt;
  20.   AutoLock.Release;
  21. end;
  22.  
  23. procedure TMyProtectedInt.SetTheInt(AValue: integer);
  24. begin
  25.   AutoLock.Acquire;
  26.   FTheInt := AValue;
  27.   AutoLock.Release;
  28. end;
  29.  
  30. initialization
  31. AutoLock := TCriticalSection.Create;
  32.  
  33. finalization
  34. AutoLock.Free;

taazz

  • Hero Member
  • *****
  • Posts: 5344
Re: First time attempting multithreading
« Reply #1 on: June 27, 2018, 02:53:21 am »
Is this cheating? Exactly which level of hell will I be banished to for spamming these getters/setters in every class I declare?  :D

Code: Pascal  [Select]
  1. type
  2.   TMyProtectedInt = object
  3.   private
  4.     FTheInt : integer;
  5.     function GetTheInt: integer;
  6.     procedure SetTheInt(AValue: integer);
  7.   public
  8.     property Value : integer read GetTheInt write SetTheInt;
  9.   end;
  10.  
  11. var
  12.   AutoLock : TCriticalSection;
  13.  
  14. implementation
  15.  
  16. function TMyProtectedInt.GetTheInt: integer;
  17. begin
  18.   AutoLock.Acquire;
  19.   result := FTheInt;
  20.   AutoLock.Release;
  21. end;
  22.  
  23. procedure TMyProtectedInt.SetTheInt(AValue: integer);
  24. begin
  25.   AutoLock.Acquire;
  26.   FTheInt := AValue;
  27.   AutoLock.Release;
  28. end;
  29.  
  30. initialization
  31. AutoLock := TCriticalSection.Create;
  32.  
  33. finalization
  34. AutoLock.Free;
No, its not cheating its a bad way to serialize access to a single resource, mostly because autolock will lock all variables based on TMyProtectedInt not only the one you try to access, but you can easily solve that by moving the autolock variable in the private section of your class (making sure you create and destroy it with your class as well). Then you have the most basic protection scheme there is.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

drallim33

  • Newbie
  • Posts: 4
Re: First time attempting multithreading
« Reply #2 on: June 27, 2018, 03:58:23 am »
Good to know

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus