Recent

Author Topic: Easier Multi-Threading for Freepascal/Lazarus?  (Read 14974 times)

j0x

  • Full Member
  • ***
  • Posts: 126
Easier Multi-Threading for Freepascal/Lazarus?
« on: August 31, 2011, 11:27:34 pm »
out of curiosity i googled how multi-threaded programming is done on VB .NET here -> http://www.programmersheaven.com/2/Les_VBNET_14_p1

and from the looks of it its much easier to do multi-threading on VBNET than lazarus freepascal so im just curious if the lazarus developers have plans to make multi-threading easier on lazarus freepascal?

or is their already some tools that makes multi-threading easier? if so please share it here

thanks

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Easier Multi-Threading for Freepascal/Lazarus?
« Reply #1 on: September 01, 2011, 12:36:42 am »
It's no easier than in FPC. How do you think it's more difficult in FPC? Just extends TThread and override Execute method, done.

There's also Parallel procedures package but its usage is unnatural for me, e.g. the parameters must be passed as pointer, etc.

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Easier Multi-Threading for Freepascal/Lazarus?
« Reply #2 on: September 03, 2011, 03:51:21 pm »
i have tried Multi-Threading in the past too -> http://forum.lazarus.freepascal.org/index.php/topic,10922.0.html
but its kinda hard for me or maybe Multi-Threaded programming is really hard?

and about parallel procedures ill look into it when i got more time

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4699
  • I like bugs.
Re: Easier Multi-Threading for Freepascal/Lazarus?
« Reply #3 on: September 03, 2011, 09:02:14 pm »
... or maybe Multi-Threaded programming is really hard?

Yes, multi-threaded programming is hard by definition.
You must synchronize the threads so they access common variables only in a controlled way.
It is not easy. Nobody claimed it is easy.
.NET does not make it any easier even if the syntax is little different.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Easier Multi-Threading for Freepascal/Lazarus?
« Reply #4 on: September 04, 2011, 03:11:29 am »
Quote
i have tried Multi-Threading in the past too but its kinda hard for me or maybe Multi-Threaded programming is really hard?
and do you think the VB .NET article you point shows that it's easier there? It doesn't describe all the bulk of multithreading (synchronization etc.). The example program there could be ported to FPC easily.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12721
  • FPC developer.
Re: Easier Multi-Threading for Freepascal/Lazarus?
« Reply #5 on: September 15, 2011, 09:51:02 pm »
out of curiosity i googled how multi-threaded programming is done on VB .NET here -> http://www.programmersheaven.com/2/Les_VBNET_14_p1

Such construct is quoted over and over again because it is relatively easy to make.

However FPC is not developed to create long feature bullet lists, but to be actually useful. And there lies the problem, this feature is great for bulletlists, and next to useless in practice.

If you feel differently, feel free to submit a patch that implements this feature.

avra

  • Hero Member
  • *****
  • Posts: 2584
    • Additional info
Re: Easier Multi-Threading for Freepascal/Lazarus?
« Reply #6 on: September 20, 2011, 01:13:54 pm »
You can try to port OmniThreadLibrary from Delphi to FPC. It has BSD license.

http://otl.17slon.com
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: Easier Multi-Threading for Freepascal/Lazarus?
« Reply #7 on: September 20, 2011, 02:02:42 pm »
There's also Parallel procedures package but its usage is unnatural for me, e.g. the parameters must be passed as pointer, etc.
MultiThreadProc is a very handy way to setup multithreading.

1. install and add MtProcs to your application's dependency list
2. uses MtProcs...
3. create a class with data to process and a method run
4. call MtProcs run function

Code: [Select]
type
 TSingleOperation=class
    FDataA,FDataB : byte;
    procedure Run;
 end;

 TMaster=class(TSomething)
    FProcessing:TList;
    procedure RunMTProc(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
    procedure SetupProcessing;
    procedure StartProcessing;
  end;

constructor TMaster.Create;
begin
  inherited;
  FProcessing:=TList.Create;
end;

procedure TMaster.SetupProcessing;
var
  aOperation:TSingleOperation;
begin
  aOperation:=TSingleOperation.Create;
  aOperation.A:=1; aOperation.B:=2;
  FProcessing.Add(aOperation);
end;

procedure TMaster.StartProcessing;
begin
  if DoRunMultiThreaded then //some flag to demonstrate running either by mtprocs or sequential
    ProcThreadPool.DoParallel(@RunMTProc,0,FProcessing.Count-1) else
 for i:=0 to FProcessing.Count-1 do
   TSingleOperation(FProcessing[i]).Run;
end;

procedure TMaster.RunMTProc(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
begin
  TSingleOperation(FProcessing[Index]).Run;
end;

procedure TSingleOperation.Run;
begin
  Result:=A+B;
end;

Take care about synchronizing data with Synchronize() or Enter/LeaveCriticalSection().
Lazarus 1.7 (SVN) FPC 3.0.0

 

TinyPortal © 2005-2018