[fpc-devel] Re: Multi threading support

Peter Popov ppopov at tamu.edu
Thu Jul 31 17:22:31 CEST 2008


I would make one comment. This type of keyworded critical sections are  
very nice. To make it really powerfull consider the following declaration:

// main function which performs a task, parts of it in parallel

procedure BigFunction(ATaskCount: Integer);

var
   fStartEvent: TEvent;

   // Local function which does parallel work:
   procedure MyFunction(X: Integer); parallel;
   begin
     // typically wait before the main function sets up whatever needs to  
be set up
     // - global vars (for BigFunction), etc.
     fStartEvent.WaitFor;
     // do something
   end;

   var
     exec_parallel: array[1..ATaskCount] of MyFunction;
   // The instantiation of ATaskCount parallel functions is done at the  
declaration of the array
   // They all hold until the fStartEvent is set in the main functions.
   // there is a problem - TEvent is a pointer to a class which is not  
instantiated at
   // declaration time

begin
   // Set up global data for parallel tasks
   fStartEvent.SetEvent;
   //
end;

This code is loosely borrowed from the way parallel code is done in ADA.  
It has been a while, but as far as I remember the  parallel features in  
ADA were really elegant and well thought-out. So I would suggest to study  
carefully the parallel syntax that is intrinsic to ADA. The key thing  
there were semaphores (critical sections) and arrays of functions which  
are run in parallel;

In order to make the above code work, semaphores and events have to become  
internal types to Pascal, so they can be instantiated at declaration time.  
One can also extend the procedure/function syntax by including a WaitFor  
feature:

...
function MyFunc(x: integer): TObject;
begin
end

....
//the following statement would call the function and then wait until it  
terminates.
  case WaitFor(MyFunc(20), time_out) of
    csSignaled: ...
    csTimeout: ...
  end;


Best regards to all.

Peter Popov

On Thu, 31 Jul 2008 04:02:28 -0500, Florian Klaempfl  
<florian at freepascal.org> wrote:

> Before we discuss endless about useless stuff, I'll make a proposal for  
> a first addition: support of the synchronized keyword. It does no more  
> than protecting a procedure automatically by a critical section so only  
> one thread can enter it. But I want to extend it. First, it can be  
> applied also to classes and object:
>
> tmyclass = class(...) synchronized
>
> This means that all classes synchronize against one critical section.
>
> Further, it allows an additional symbol to be given
>
> procedure p;synchronized mycriticialsection;
>
> it must be of the type System.TRTLCriticalSection and the procedure will  
> be synchronized against this critical section.
>
> Besides saving the typing of endless try ... finally statements and the  
> initialiation of the unnamed critical sections, it creates also slightly  
> better code because the LeaveCriticalSection call can be inside the  
> implicit exception frame which is created anyways.
>
> Any suggestions about doing it better?
>
> BTW: I'll ignore comments like "nobody needs this", I need it :)
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel



-- 
|***********************************************|
|                                               |
|  Peter Popov,                                 |
|                                               |
|  608L Blocker Bldg.                           |
|  Institute for Scientific Computation,        |
|  Texas A&M University                         |
|  College Station, Texas 77843-3404            |
|-----------------------------------------------|
|  Phone: +1 (979) 458-4644,                    |
|  Fax: +1 (979) 845-5827                       |
|                                               |
|***********************************************|



More information about the fpc-devel mailing list