Recent

Author Topic: Java/Python style "threading" Unit in FPC?  (Read 2354 times)

kveroneau

  • Jr. Member
  • **
  • Posts: 72
Java/Python style "threading" Unit in FPC?
« on: March 30, 2016, 08:11:23 pm »
I was thinking of developing a threading unit, and Java/Python like Thread class in FPC.  However, before diving into porting that threading code over from Python(as I can read and port Python code easier), I wanted to see if this type of unit has already been made by someone else?

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
https://docs.python.org/2/library/threading.html
https://hg.python.org/cpython/file/2.7/Lib/threading.py

Basically, the unit would expose a "Thread" class, which should normally be subclassed/extended, and the "run()" procedure should be overridded to contain the actual code which will run within the thread.  The class would then be created on the Heap, and started using the "start()" procedure.

I have been using Python for a very long time, and I really enjoy how easy it is to create and manage thread classes, and would love to use the same type of technology in FPC/Lazarus applications.  If someone else has already ported the Java/Python "Thread" class to FPC/Lazarus, could you please point me to where I could obtain the Unit file?  If not, I plan on porting over the Python version over to FPC/Lazarus using it's source code as a model.  Python links to the pthreads internally, and from what I have read about FPC's threading, the low-level API of Python threading is very similar to the threading in FPC.  So porting it shouldn't be too difficult.

Cyrax

  • Hero Member
  • *****
  • Posts: 755
Re: Java/Python style "threading" Unit in FPC?
« Reply #1 on: March 30, 2016, 08:57:54 pm »
There is already TThread class in FPC.

See documentation : http://www.freepascal.org/docs-html/rtl/classes/tthread.html

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7451
Re: Java/Python style "threading" Unit in FPC?
« Reply #2 on: March 30, 2016, 09:10:33 pm »
The only thing is that "run"  is called "Execute", and must be declared with override (since not all methods are virtual)

kveroneau

  • Jr. Member
  • **
  • Posts: 72
Re: Java/Python style "threading" Unit in FPC?
« Reply #3 on: March 30, 2016, 09:44:39 pm »
Awesome, thank you for the rather swift replies.  I thought I saw a class like this as I was looking through the docs before.  Read the multi-threading wiki page, and TThread even has some interesting advantages over the Java/Python classes which make synchronizing with the main thread and other threads much less of a hassle.  One huge downside of threads in Python is the GIL(Global Interpreter lock), so the use of the threads in Python is limited in some situations.

avra

  • Hero Member
  • *****
  • Posts: 1689
    • Additional info
« Last Edit: August 28, 2019, 09:52:12 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 8937
Re: Java/Python style "threading" Unit in FPC?
« Reply #5 on: August 28, 2019, 11:02:17 am »
You might also be interested in the class methods of TThread. These make it as easy as Python to run a task on a thread. It is actually semantically the same or close to how Java handles simple multi-threading.
Note that truly native compiled languages have a disadvantage in syntax compared to Python or Java, which are not really comparable because of that.
« Last Edit: August 28, 2019, 11:05:48 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.