Recent

Author Topic: Help with multiple threads  (Read 4307 times)

daringly

  • Jr. Member
  • **
  • Posts: 73
Help with multiple threads
« on: February 22, 2017, 06:40:45 pm »
I tried installing the mtprocs unit, and had no luck. I downloaded multithreadprocslaz from Source forge, and either I'm an idiot, or it was incomplete.

I want to do math calculations in parallel, since I have several computers with at least 8 cores.

What is the simplest way to run these two calls at the same time?

Answer[1]:=doSomeMath(datarecord[1]); // use one core or thread
Answer[2]:=doSomeMath(datarecord[2]); // use another core or thread

If I can learn how to do that, I hope it is simple to expand to more cores, and running many more datarecords/answers.

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Help with multiple threads
« Reply #1 on: February 22, 2017, 08:13:12 pm »
That looks like linux, right ? I can only show how I would do it in windows, maybe it helps. This is very crude, no error handling, thread termination etc.

{$mode objfpc}

type TMath = Class
       //var xyz... keep all working variables local to the Class
       constructor Create;
       procedure DoSomeMath;
     end;

function ThreadFunc (p: pointer): Ptrint;
begin
  TMath(p).DoSomeMath;
end;


var ThreadID : array [1..2] of longint;
    MathUnits: array [1..2] of TMath;

var n: longint;

begin
  for n := 1 to 2 do begin
    MathUnits[n] := TMath.Create;
    ThreadID[n] := BeginThread (@ThreadFunc, MathUnits[n]);
  end;

//Threads are now up and running and we can lay back and wait for a keypress
  repeat
    sleep (20);
  until some event....

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Help with multiple threads
« Reply #2 on: February 22, 2017, 09:27:20 pm »
That looks like linux, right ? I can only show how I would do it in windows, maybe it helps. This is very crude, no error handling, thread termination etc.

{$mode objfpc}

type TMath = Class
       //var xyz... keep all working variables local to the Class
       constructor Create;
       procedure DoSomeMath;
     end;

function ThreadFunc (p: pointer): Ptrint;
begin
  TMath(p).DoSomeMath;
end;


var ThreadID : array [1..2] of longint;
    MathUnits: array [1..2] of TMath;

var n: longint;

begin
  for n := 1 to 2 do begin
    MathUnits[n] := TMath.Create;
    ThreadID[n] := BeginThread (@ThreadFunc, MathUnits[n]);
  end;

//Threads are now up and running and we can lay back and wait for a keypress
  repeat
    sleep (20);
  until some event....

I would replace BeginThread and custom class with TThread and thread queues...

daringly

  • Jr. Member
  • **
  • Posts: 73
Re: Help with multiple threads
« Reply #3 on: February 23, 2017, 12:15:12 am »
Windows 10, using Lazarus IDE.

daringly

  • Jr. Member
  • **
  • Posts: 73
Re: Help with multiple threads
« Reply #4 on: February 23, 2017, 03:06:03 pm »
I'm clearly missing the big picture.

I have the multithreadprocslaz package file on my desktop.

I tried following the directions here: http://wiki.freepascal.org/Parallel_procedures

When I try to run the test code, the compiler cannot find MTProcs.

How exactly to I get the MTProcs unit into my lazarus library?

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Help with multiple threads
« Reply #5 on: February 23, 2017, 04:37:58 pm »
I don't quite understand why you need these packages. As said above, you can get simple multithreading under windows to work without any additional packages.
I took this approach for my simulation program, and it works perfectly. You were asking for a simple solution and that is it.



 

TinyPortal © 2005-2018