Sign in Join

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

Mailing lists / lazarus / 2010-08

Multithreadprocs and breaking out of loop

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Juha Manninen gmail

Hi

I tried multithreadprocs package, explained in wiki: http://wiki.lazarus.freepascal.org/Parallel_procedures

I works well. Cool!

Question: how to break out of a paralled loop in a clean way?

The problem is finding something from blocks of data. There are more blocks than there are threads. It would be enough to prevent new threads starting. The currently running threads can run to the end.

Should I throw an exeption and then catch it outside the ProcThreadPool.DoParallel(...) call?

The best solution could be added to the wiki page, too.

Juha

Mattias Gaertner
to Juha Manninen gmail
Show quoted text 15 lines
Hi I tried multithreadprocs package, explained in wiki: http://wiki.lazarus.freepascal.org/Parallel_procedures I works well. Cool! Question: how to break out of a paralled loop in a clean way? The problem is finding something from blocks of data. There are more blocks than there are threads. It would be enough to prevent new threads starting. The currently running threads can run to the end. Should I throw an exeption and then catch it outside the ProcThreadPool.DoParallel(...) call?

Yes, that works.

The best solution could be added to the wiki page, too.

I doubt there is one best solution for all problems.

You could use a boolean flag Abort somewhere. It does not even need a critical section.

Mattias

Juha Manninen gmail
to Mattias Gaertner

Thanks.

What about updating GUI in main thread, like normally done with TThread.Synchronize? I tried this:

  Item.Thread.Synchronize(Item.Thread, @ShowStatus);

where ShowStatus is part of the class calling the parallel funcs. It doesn't work --> "RunError(210)".

Juha

Felipe Monteiro de Carvalho
to Juha Manninen gmail

OS, FPC and Lazarus versions?

But I would guess that you didn't add cthreads to your uses clause to add threading support under UNIX.

Felipe Monteiro de Carvalho
Juha Manninen gmail
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote
OS, FPC and Lazarus versions? But I would guess that you didn't add cthreads to your uses clause to add threading support under UNIX.

FPC 2.4.0. Fedora 13 Linux.

Yes, I actually forgot cthreads. Now I added it and tested again. The same error came still. I remember that missing cthreads caused some other type of error.
Anyway, it is not serious. It is difficult to update GUI from parallel methods anyway because they are ... parallel.

Juha

Felipe Monteiro de Carvalho
to Juha Manninen gmail

Synchronize should work for sure.

I took a quick look and it is: 210 Object not initialized

It's hard to say without seeing your code, but I guess there is a bug in your code somewhere.

Another guess would be that this code is executed before the initialization of cthreads.

Felipe Monteiro de Carvalho
Juha Manninen gmail
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 9 lines
Synchronize should work for sure. I took a quick look and it is: 210 Object not initialized It's hard to say without seeing your code, but I guess there is a bug in your code somewhere. Another guess would be that this code is executed before the initialization of cthreads.
procedure TCombBlock.ShowStatus;
begin
  ;
end;
procedure TCombBlock.SolveSubBlocks(aThreadIndex: PtrInt; Data: Pointer; Item:
TMultiThreadProcItem);
begin
  Item.Thread.Synchronize(Item.Thread, @ShowStatus); // <-- this gives error!
  ...
end;
function TCombBlock.SolveFirst: Boolean;
begin
...
    fOwner.fThreadPool.DoParallel(@SolveSubBlocks,1,fFindInd-1,nil);
...
end;

-------------

I removed the other lines from the functions for this example. Only "Item.Thread.Synchronize(..." gives the problem. fOwner is a normal class derived from TObject.
I debugged to see the problem. It turned out "Item.Thread" is nil! So the problem is actually that Multithreadprocs does not pass the underlying thread object for a parallel function.

Juha