Recent

Author Topic: Simple multithreading TWThread, TWCThread  (Read 13083 times)

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Simple multithreading TWThread, TWCThread
« on: October 21, 2016, 02:26:49 pm »
Hello.

This is my component to work with additional threads for Delphi & Lazarus.
Using them is very easy.
Drop a TWCThread on the form and add a few tasks.
In the task you need to write a method OnExecute which will be executed in a different thread.
Each task is a message flow. All tasks will run in the start order.

Components tested on Ubuntu 14 and 16 LTS, Windows 7, WinCE, Lazarus 1.6/1.7, Delphi 7/XE2.

The archive contain a small demos.


[updated 26 nov 2016]
Rewrite code for memory leaks.

Added two new demo:
1. Small& simple update system from local server (share) to local computer. It uses two threads: one for reading and one for writing (for speed). After update auto run main file. Update settings are specified in ini file. It should work on Linux, I have not tested it.
Quote
[Main]
# caption for run.exe
Name=SomeExe
# main file, will run after update
Run=SomeExe.exe
# cmdLine for main file
Parameters=
# count of files, see above
Count=1
#always update/overwrite
Overwite=1

[File1]
# full path of source file
From=run.exe
# short (local dir) of dest file
To=run.tmp
2. Background lazReport generate and printing.

For the correct execution of this demo need to make some corrections in LR_CLass.pas of lazReport package file.
It is necessary to exclude the call Application.ProcessMessages.

Add option to a report item roMultithread:
Code: [Select]
TfrReportOption = (roIgnoreFieldNotFound, roIgnoreSymbolNotFound, roHideDefaultFilter,
                     roDontUpgradePreparedReport,   // on saving an old prepared report don't update to current version
                     roSaveAndRestoreBookmarks,     // try to save and later restore dataset bookmarks on building report
                     roPageHeaderBeforeReportTitle,  // PageHeader band is printed before ReportTitle band
                     roMultithread // this
                     );   
Add protected method procedure DoApplicationProcessMessages to TfrReport class;
Code: [Select]
procedure TfrReport.DoApplicationProcessMessages;
begin
  if (not (roMultithread in Options)) or (csDesigning in ComponentState) then
    Application.ProcessMessages;
end;
And then replace all the code
Code: [Select]
Application.ProcessMessages;with the following code
Code: [Select]
if Assigned(CurReport) then
  CurReport.DoApplicationProcessMessages;
Thats all, your report ready to run and print in background! :)
It is enough to turn on [roMultithread] and off ShowProgress.
Archive contain modified version of LR_CLass.pas from latest version (from github).


[updated 28 nov 2016]

Unfortunately demo report failed to run under the nix. :(
In Windows7 it works fine.


---


Some fixes. :)


---
This component is published on https://github.com/wadman/wthread/
« Last Edit: July 10, 2017, 01:27:45 pm by wadman »

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Simple multithreading TWThread, TWCThread
« Reply #1 on: October 21, 2016, 03:40:12 pm »
Runs nicely. (Win 10 x64/Laz 1.6RC1/fpc 3.0.0)  I like the custom task property editor.
A multi-threading demo would be nice.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Re: Simple multithreading TWThread, TWCThread
« Reply #2 on: October 22, 2016, 08:25:07 pm »
minesadorada, thank u for testing.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Simple multithreading TWThread, TWCThread
« Reply #3 on: October 23, 2016, 01:30:40 am »
It doesn't compile under FreeBSD. I tested with FPC 2.6.4 under 64-bit FreeBSD 10.3 and Lazarus (trunk).

Code: [Select]
Compile package Threading 1.0: Exit code 1, Errors: 2, Hints: 4
Hint: Start of reading config file /home/graemeg/.fpc.cfg
Hint: End of reading config file /home/graemeg/.fpc.cfg
Verbose: Free Pascal Compiler version 2.6.4 [2014/02/28] for x86_64
Verbose: Copyright (c) 1993-2014 by Florian Klaempfl and others
Verbose: Target OS: FreeBSD for x86-64
Verbose: Compiling threading.pas
Verbose: Compiling wcthread.pas
Verbose: Compiling wthread.pas
wthread.pas(816,45) Error: Incompatible type for arg no. 1: Got "Pointer", expected "QWord"
wthread.pas(333,10) Hint: Found declaration: pthread_getaffinity_np(QWord,LongInt,Pointer):LongInt; CDecl;
wthread.pas(850,52) Error: Incompatible type for arg no. 1: Got "TThreadID", expected "QWord"
wthread.pas(332,10) Hint: Found declaration: pthread_setaffinity_np(QWord,LongInt,Pointer):LongInt; CDecl;
wthread.pas(1251,0) Verbose: There were 2 errors compiling module, stopping
Verbose: Compilation aborted
Verbose: /data/devel/fpc-2.6.4/x86_64-freebsd/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

mica

  • Full Member
  • ***
  • Posts: 196
Re: Simple multithreading TWThread, TWCThread
« Reply #4 on: October 23, 2016, 02:38:07 am »
Works with OpenSuse 13.2 x86/64bit

more demos and docu would be nice.

Thaddy

  • Hero Member
  • *****
  • Posts: 14357
  • Sensorship about opinions does not belong here.
Re: Simple multithreading TWThread, TWCThread
« Reply #5 on: October 23, 2016, 09:58:05 am »
It doesn't compile under FreeBSD. I tested with FPC 2.6.4 under 64-bit FreeBSD 10.3 and Lazarus (trunk).
It does. It doesn't compile with outdated unsupported compilers like 2.6.4. But you can still use 2.6.4 to bootstrap a 3.X on FreeBSD. You know that.
Quite a lot has changed with threading.

Btw: I like the message model here over the TSimpleThread solution that was added to the rtl in classes.
« Last Edit: October 23, 2016, 10:05:51 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Simple multithreading TWThread, TWCThread
« Reply #6 on: October 23, 2016, 05:31:53 pm »
It does. It doesn't compile with outdated unsupported compilers like 2.6.4.
Sorry to burst your bubble, but FPC 2.6.4 is still extensively used by many - even core FPC team member's place of work. Also the original post in this thread mentions tested on Delphi 7 and XE2. Well, Delphi 7 pre-dates FPC 2.6.4 by many, many years. So why is FPC 2.6.4 considered "unsupported or out of date" when Delphi 7 isn't?

Quote
But you can still use 2.6.4 to bootstrap a 3.X on FreeBSD. You know that.
Quite a lot has changed with threading.
Next time, maybe do your own testing before you comment. I just tested with FPC 3.0.0 under 64-bit FreeBSD 10.3 and I get the exact same problem as with FPC 2.6.4.  Please remember... Linux is NOT FreeBSD. The Free Pascal RTL code also varies between those two platforms.

The results with FPC 3.0.0
Code: [Select]
Compile package Threading 1.0: Exit code 1, Errors: 2, Hints: 4
Hint: Start of reading config file /home/graemeg/.fpc.cfg
Hint: End of reading config file /home/graemeg/.fpc.cfg
Verbose: Free Pascal Compiler version 3.0.0 [2015/11/16] for x86_64
Verbose: Copyright (c) 1993-2015 by Florian Klaempfl and others
Verbose: Target OS: FreeBSD for x86-64
Verbose: Compiling threading.pas
Verbose: Compiling wcthread.pas
Verbose: Compiling wthread.pas
wthread.pas(816,45) Error: Incompatible type for arg no. 1: Got "Pointer", expected "QWord"
wthread.pas(333,10) Hint: Found declaration: pthread_getaffinity_np(QWord;LongInt;Pointer):LongInt; CDecl;
wthread.pas(850,52) Error: Incompatible type for arg no. 1: Got "TThreadID", expected "QWord"
wthread.pas(332,10) Hint: Found declaration: pthread_setaffinity_np(QWord;LongInt;Pointer):LongInt; CDecl;
wthread.pas(1251,0) Verbose: There were 2 errors compiling module, stopping
Verbose: Compilation aborted
Verbose: /data/devel/fpc-3.0.0/x86_64-freebsd/bin/ppcx64 returned an error exitcode


And before you tell me to upgrade to FPC 3.1.1 (trunk), the same compiler error happens with FPC 3.1.1 too. Like I said, Linux is not FreeBSD. Sometimes there are slight differences.

Code: [Select]
Compile package Threading 1.0: Exit code 1, Errors: 2, Hints: 4
Hint: Start of reading config file /home/graemeg/.fpc.cfg
Hint: End of reading config file /home/graemeg/.fpc.cfg
Verbose: Free Pascal Compiler version 3.1.1 [2016/10/14] for x86_64
Verbose: Copyright (c) 1993-2016 by Florian Klaempfl and others
Verbose: Target OS: FreeBSD for x86-64
Verbose: Compiling threading.pas
Verbose: Compiling wcthread.pas
Verbose: Compiling wthread.pas
wthread.pas(816,45) Error: Incompatible type for arg no. 1: Got "Pointer", expected "QWord"
wthread.pas(333,10) Hint: Found declaration: pthread_getaffinity_np(QWord;LongInt;Pointer):LongInt; CDecl;
wthread.pas(850,52) Error: Incompatible type for arg no. 1: Got "TThreadID", expected "QWord"
wthread.pas(332,10) Hint: Found declaration: pthread_setaffinity_np(QWord;LongInt;Pointer):LongInt; CDecl;
wthread.pas(1251,0) Verbose: There were 2 errors compiling module, stopping
Verbose: Compilation aborted
Verbose: /data/devel/fpc-3.1.1/x86_64-freebsd/bin/ppcx64 returned an error exitcode
Btw: I like the message model here over the TSimpleThread solution that was added to the rtl in classes.
[/quote]


From the FreeBSD man pages, the interface API for pthread_getaffinity_np() looks as follows. I don't know how that differs to Linux's pthread library - I don't use Linux.

Code: C  [Select][+][-]
  1.      #include <pthread_np.h>
  2.  
  3.      int
  4.      pthread_getaffinity_np(pthread_t td, size_t cpusetsize, cpuset_t *cpusetp);
  5.  
  6.      int
  7.      pthread_setaffinity_np(pthread_t td, size_t cpusetsize, const cpuset_t *cpusetp);
  8.  

http://www.unix.com/man-page/freebsd/3/pthread_getaffinity_np/
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 14357
  • Sensorship about opinions does not belong here.
Re: Simple multithreading TWThread, TWCThread
« Reply #7 on: October 23, 2016, 07:36:39 pm »
I usually test my suggestions as you are well aware. Here I did not. My apologies. I will test it, however, after the COTA.

About 2.6.4: it REALLY is unsupported.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Re: Simple multithreading TWThread, TWCThread
« Reply #8 on: October 24, 2016, 09:19:54 am »
It doesn't compile under FreeBSD. I tested with FPC 2.6.4 under 64-bit FreeBSD 10.3 and Lazarus (trunk).


Please, show type definition of ptruint

Code: [Select]
function pthread_setaffinity_np(pid: PtrUInt; cpusetsize: LongInt; cpuset: Pointer): LongInt; cdecl; external;It looks like this?
Code: [Select]
{$ifdef CPU64}
  SizeInt = Int64;
  SizeUInt = QWord;
  PtrInt = Int64;
  PtrUInt = QWord;
  ValSInt = int64;
  ValUInt = qword;
  CodePointer = Pointer;
  CodePtrInt = PtrInt;
  CodePtrUInt = PtrUInt;
{$endif CPU64}


{$ifdef CPU32}
  SizeInt = Longint;
  SizeUInt = DWord;
  PtrInt = Longint;
  PtrUInt = DWord;
  ValSInt = Longint;
  ValUInt = Cardinal;
  CodePointer = Pointer;
  CodePtrInt = PtrInt;
  CodePtrUInt = PtrUInt;
{$endif CPU32} 

Thaddy

  • Hero Member
  • *****
  • Posts: 14357
  • Sensorship about opinions does not belong here.
Re: Simple multithreading TWThread, TWCThread
« Reply #9 on: October 24, 2016, 10:01:08 am »
Please, show type definition of ptruint
If Graeme says there is a problem, then there usually is a problem. Be careful asking him for the obvious. That's not needed. (apart from using 2.6.4 ;) )
I am half way installing a FreeBSD system on a VM.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Simple multithreading TWThread, TWCThread
« Reply #10 on: October 24, 2016, 10:10:43 am »
About 2.6.4: it REALLY is unsupported.
Well, FPC 3.0.0 was released with some glaring bugs. It wasn't a good release, hence they are rushing to get 3.0.2 out. FPC 2.6.4 was a very stable release, and I'll be using it (for personal and commercial work) until 3.0.2 is out.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Simple multithreading TWThread, TWCThread
« Reply #11 on: October 24, 2016, 10:16:45 am »
If Graeme says there is a problem, then there usually is a problem. Be careful asking him for the obvious.
You make me sound bad.  :-[ Normally I don't mind investigating such issues myself, especially as I'm interested in this subject... threading. Unfortunately I'm a bit swamped with work, so can't take on another task. Hence I simply reported my issue under FreeBSD.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 14357
  • Sensorship about opinions does not belong here.
Re: Simple multithreading TWThread, TWCThread
« Reply #12 on: October 24, 2016, 10:29:13 am »
Feel good. I take you serious. Very! And I owe you one.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Simple multithreading TWThread, TWCThread
« Reply #13 on: October 24, 2016, 10:41:23 am »
Please, show type definition of ptruint
That is not the problem. The definition under FreeBSD, for those types, are identical to Linux.

Quote
Code: [Select]
function pthread_setaffinity_np(pid: PtrUInt; cpusetsize: LongInt; cpuset: Pointer): LongInt; cdecl; external;
This definition seems to be the problem. You replaced the parameter types with base types, but maybe FreeBSD expects something different.

Also from the man pages of both FreeBSD and Linux, the first parameter is not a PID (process ID) value, but a Thread ID value. Off the top of my head, I am unsure if they have the same type definition.

Linux and FreeBSD man pages:

  https://www.freebsd.org/cgi/man.cgi?query=pthread_setaffinity_np&apropos=0&sektion=0&manpath=FreeBSD+10.3-RELEASE&arch=default&format=html

  http://www.unix.com/man-page/linux/3/pthread_setaffinity_np/

I think part of the problem is that it seems all functions defined in pthread_np.h are Non-POSIX functions. So not really standardized, thus could differ from platform to platform.

Sorry, this is all the time I can spend on this subject at the moment.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11444
  • FPC developer.
Re: Simple multithreading TWThread, TWCThread
« Reply #14 on: October 24, 2016, 04:19:49 pm »
About 2.6.4: it REALLY is unsupported.
Well, FPC 3.0.0 was released with some glaring bugs. It wasn't a good release, hence they are rushing to get 3.0.2 out.

It was mostly fine except for the currency problem. I don't use currency and for me 3.0 was a blessing. Much less IEs etc than with 2.6.4

 

TinyPortal © 2005-2018