Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-05

How can I write a multithread program as simple as possible?

9 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Géza Kovacs Géza

Hi!

I read the Wiki about this article, it's looks like very complicated for me. :-( Can anybody show me a very simple example?
Below is a code, i would like to run it multithread both under Windows and Linux.

Program Sorting;
const
	n = 999000;
var
	data : array [1..n] of integer;
	i, j, t: LongInt;
begin
		for i := 1 to n do
			data[i]:=random(32768);
		for i := 1 to n-1 do
		begin
			for j:=i+1 to n do
			begin
				if data[i]<data[j] then
				begin
					t:=data[i];
					data[i]:=data[j];
					data[j]:=t;
				end;
			end;
		end;
end.

Best regards, G

Tomas Hajny
to Géza Kovacs Géza

On Tue, May 26, 2015 18:12, Géza Kovacs Géza wrote:

Hi!

Show quoted text 28 lines
I read the Wiki about this article, it's looks like very complicated for me. :-( Can anybody show me a very simple example? Below is a code, i would like to run it multithread both under Windows and Linux. Program Sorting; const n = 999000; var data : array [1..n] of integer; i, j, t: LongInt; begin for i := 1 to n do data[i]:=random(32768); for i := 1 to n-1 do begin for j:=i+1 to n do begin if data[i]<data[j] then begin t:=data[i]; data[i]:=data[j]; data[j]:=t; end; end; end; end.

What exactly shall be processed in parallel ("multithreaded") according to your expectation? FPC doesn't provide any means for automated splitting complex task into multiple smaller tasks spawned in parallel threads if that's what you meant.

Tomas

leledumbo
to Géza Kovacs Géza: Hi! I read the Wiki about this article, it's looks like very complicated for me. :-( Can…
I read the Wiki about this article, it's looks like very complicated for

me. :-(

Can anybody show me a very simple example?

Well, it surely is not a beginner's topic. Tell me, if I give:

type
  TMyThread = class(TThread)
    procedure Execute; override;
  end;
procedure TMyThread.Execute;
begin
  WriteLn(i);
end;
begin
  with TMyThread.Create(true) do begin
    FreeOnTerminate := true;
    Start;
  end;
end.

does it make sense to you?

Michael Schnell
to Géza Kovacs Géza: Hi! I read the Wiki about this article, it's looks like very complicated for me. :-( Can…
Géza Kovacs Géza wrote
I read the Wiki about this article, it's looks like very complicated for me. :-(

AFAIK, there are examples on how to create TThreads in the fpc Wiki.

OTOH performance optimization via threads on multi-Core systems is not the original target of thread programming, but of course it's a decent way t make use of it.

Delphi does provide easy to use library calls for this (using the "Anonymous" syntax). AFAIK, fpc does have the "Anonymous" syntax since some time, and hopefully the library will be enhanced for such "parallel loops" some day soon.

Before this, you need to create the threads using TThread.

In fact doing a thread pool would be more appropriate.

I already did a workable draft of a thread pool unit that is usable for such tasks, but provided that a Delphi compatible "Anonymous parallel loop" library might be the better way to go I stopped working on that.

-Michael

Sven Barth
to Michael Schnell
"Michael Schnell" wrote
Delphi does provide easy to use library calls for this (using the

"Anonymous" syntax). AFAIK, fpc does have the "Anonymous" syntax since some time, and hopefully the library will be enhanced for such "parallel loops" some day soon.

Where did you get the idea that we already have that? Someone /is/ working on it (or is even finished), but nothing is in trunk yet.

Regards,
Sven

Michael Schnell
to Sven Barth
Sven Barth wrote
Where did you get the idea that we already have that? Someone /is/ working on it (or is even finished), but nothing is in trunk yet.

We discussed this in the fpc-devel List and I learned that Delphi has the "anonymous" syntax since D2009. It was mentioned in several discussions.

So I had the - seemingly wrong - impression that it already is available.

-Michael

Géza Kovacs Géza
to Michael Schnell

Hi!

Can anybody send me a "Pascal Multithreading tutorial for beginners" or similar document (if it exist)?
What is the simplest method to learn and understand it?

Best regards, G