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

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Tomas Hajny-2
On Tue, May 26, 2015 18:12, Géza Kovacs Géza wrote:


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.

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

leledumbo
Administrator
In reply to this post by Géza Kovacs Géza
> 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?
Reply | Threaded
Open this post in threaded view
|

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

Michael Schnell
In reply to this post by Géza Kovacs Géza
On 05/26/2015 06:12 PM, 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Sven Barth-2

Am 27.05.2015 09:49 schrieb "Michael Schnell" <[hidden email]>:
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Michael Schnell
On 05/28/2015 12:30 AM, 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Géza Kovacs Géza
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Antonio Sanguigni
2015-05-30 6:28 GMT+02:00 Géza Kovacs Géza <[hidden email]>:
> Hi!
>
> Can anybody send me a "Pascal Multithreading tutorial for beginners"
> or similar document (if it exist)?

I like this one. Old but ideas are there.

http://thaddy.co.uk/threads/

Antonio



--
Antonio Sanguigni alias slapshot
----------------------------------------------------------------------
Servizi informatici Windows e GNU/Linux
http://www.backupsolutions.it
http://www.pieroni.biz
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Marco van de Voort
In reply to this post by Géza Kovacs Géza
In our previous episode, G?za Kovacs G?za said:
> Can anybody send me a "Pascal Multithreading tutorial for beginners"

Search for Martin Harvey (and "Delphi" "threads")
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal