Recent

Author Topic: multithreading learning curve  (Read 21802 times)

Graeme

  • Hero Member
  • *****
  • Posts: 1527
    • Graeme on the web
Re: multithreading learning curve
« Reply #15 on: August 14, 2015, 01:28:36 pm »
SendMessage and PostMessage on Windows. But what is the equivalent on Linux?
Some cross-platform toolkits - I mention no names  ;) - has cross-platform versions of SendMessage and PostMessage, and they post to an internal application event queue which is thread-safe.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

derek.john.evans

  • Guest
Re: multithreading learning curve
« Reply #16 on: August 14, 2015, 01:53:00 pm »
TIdThreadSafeStringList is fantastic for strings. (Indy)

TThreadList (Classes) can work as a queue. A TJPEGImage would need to be created for each load and the main thread would free the object after it assigns it to a TImage.

In the past Ive used Indy's TIdThreadSafeStringList for sending string messages. TIdThreadSafeList extends TThreadList with Pop/Pull methods.
TIdThreadSafeObjectList extends TIdThreadSafeList with object ownership, which kicks in if you free the thread list before its empty.

But, im unsure why it would be needed here, unless you want to throttle the GUI renderings to a frame rate, which would mean, the thread loads JPEG's which arn't shown.

But, then that is more of a buffering feature, in which case you wouldn't use a queue. TThreadList would be a map of the frames and the JPEG's would come from another TThreadList which acts as a JPEG cache. Which would mean, JPEG's are never free-ed until the program ends. They just get reused.

balazsszekely

  • Guest
Re: multithreading learning curve
« Reply #17 on: August 14, 2015, 02:00:33 pm »
Quote
@JD
True. SendMessage and PostMessage on Windows.
PostMessage yes, SedMessage no. The first one is asynchronous, the later one is a blocking function.

Quote
@JD
But what is the equivalent on Linux?
Mutexes.

Quote
@user5
I know that most folks endorse Synchronize(@), but not everyone
No one endorse Synchronize(@), that's the whole point. If you want to update visual controls from a thread, synchronize is the worse, working solution possible.

Quote
@user5
"MyThread.FreeOnTerminate := true;" would automatically cause the thread to free itself when it finishes.
Yes, after the execute procedure is over, the thread will free itself. But be aware that creating + freeing threads takes time. In you case, I think the best solution is to create a few threads that run continuously and feeds a thread safe list.

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: multithreading learning curve
« Reply #18 on: August 18, 2015, 09:54:28 am »
Quote
@JD
But what is the equivalent on Linux?
Mutexes.

Quote
@user5
I know that most folks endorse Synchronize(@), but not everyone
No one endorse Synchronize(@), that's the whole point. If you want to update visual controls from a thread, synchronize is the worse, working solution possible.

Quote
@user5
"MyThread.FreeOnTerminate := true;" would automatically cause the thread to free itself when it finishes.
Yes, after the execute procedure is over, the thread will free itself. But be aware that creating + freeing threads takes time. In you case, I think the best solution is to create a few threads that run continuously and feeds a thread safe list.

I've used semaphores (on Windows) but not mutexes.

I've also noticed a delay when threads are left to free themselves. I thought of using KillThread to force the process but it seems to defeat the whole purpose of FreeOnTerminate := True.

I use synchronize extensively & I know it's not everyone's cup of tea but there are no simpler options.

JD
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12905
  • FPC developer.
Re: multithreading learning curve
« Reply #19 on: August 18, 2015, 11:22:48 am »
PS:
The synchronize method is not perfect either, it will cause the thread to wait until the synchronization is done. There are better ways...depending on platform you're using.

True. SendMessage and PostMessage on Windows. But what is the equivalent on Linux? I would love to find out.

Closest come resp. TThread.Synchronize and TThread.Queue.  The latter is FPC3+

Usually I schedule a method that pulls other data from a threadsafe queue based on tthreadlist.

I check the count of the tthreadlist queue to see if the mainthread is not too busy. (messages outstanding).

Note that you can only send messages from a thread to the mainthread this way while the *message functions can send messages to any handle.

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: multithreading learning curve
« Reply #20 on: August 18, 2015, 11:49:39 am »
Hi
Threading is difficult, another way of thinking  :D
Why not "just" roll your own way of communicating????
I've got a VERY messy testproject somewhere, if someone is interested, I had to learn too  %)
Let me know - Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: multithreading learning curve
« Reply #21 on: August 18, 2015, 01:25:47 pm »
Alternatively use C# or even better Haskell where multithreading is automated on a higher level so you don't have to take care of many low-level details
Too late to escape fate

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: multithreading learning curve
« Reply #22 on: August 18, 2015, 03:45:53 pm »
Closest come resp. TThread.Synchronize and TThread.Queue.  The latter is FPC3+

Usually I schedule a method that pulls other data from a threadsafe queue based on tthreadlist.

I check the count of the tthreadlist queue to see if the mainthread is not too busy. (messages outstanding).

Note that you can only send messages from a thread to the mainthread this way while the *message functions can send messages to any handle.

How can I get FPC 3+? I don't want to use fpcup because I don't want to mess up my existing installation & besides I want it to have the same directory structure as FPC 2.6.4 in Lazarus 1.4.2. That way I can compile projects with either compiler just by changing compiler paths.
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

balazsszekely

  • Guest
Re: multithreading learning curve
« Reply #23 on: August 18, 2015, 04:12:41 pm »
Quote
How can I get FPC 3+? I don't want to use fpcup because I don't want to mess up my existing installation & besides I want it to have the same directory structure as FPC 2.6.4 in Lazarus 1.4.2. That way I can compile projects with either compiler just by changing compiler paths.
AFAIK fpcup install everything in the "...\development" directory and does not interfere with you current installation. Another solution would be a virtual machine.
« Last Edit: August 18, 2015, 08:15:34 pm by GetMem »

Graeme

  • Hero Member
  • *****
  • Posts: 1527
    • Graeme on the web
Re: multithreading learning curve
« Reply #24 on: August 18, 2015, 06:57:49 pm »
How can I get FPC 3+? I don't want to use fpcup because I don't want to mess up my existing installation

Use svn to checkout the 3.0.x rc branch. Compile it using FPC 2.6.4 and install in in your $HOME directory. My suggested layout is described in the following mailing list post.

http://lists.freepascal.org/pipermail/fpc-pascal/2015-August/044922.html

It is not difficult to compile a newer FPC. I have a script I've been using for years. This is for FreeBSD, but Linux is virtually the same.

Code: [Select]
#!/usr/local/bin/bash

TARGET=x86_64-freebsd
COMPILER=/data/devel/fpc-2.6.4/$TARGET/bin/ppcx64
NEWFPC=3.0.1

cd src

gmake clean
gmake all FPC=$COMPILER OPT="-Fl/usr/local/lib"
gmake install INSTALL_PREFIX=/data/devel/fpc-$NEWFPC/$TARGET FPC=/data/devel/fpc-$NEWFPC/src/compiler/ppcx64
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: multithreading learning curve
« Reply #25 on: August 18, 2015, 07:32:48 pm »
I started this thread to learn about how multithreading might help me to 'speed up' one of my program's video layback options (the other using MPlayerControl) and all of the responses have been instructive but I've always wanted to also try VLC.

Unfortunately, VLC won't work with my version of Lazarus (1.1.10). Once upon a time I did install Lazarus 1.2.0 but I was surprised to find that the Object Inspector didn't contain anything. I suppose this means that I have to fill it in myself but I don't know how to do that. Is the newest version of Lazarus the same way and if so, can anyone direct me to where I might learn how to install the Object Inspector's properties?

I don't want to start a new thread on this one simple (or simpleton) issue. I've done a lot of searching about this but I haven't found much.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: multithreading learning curve
« Reply #26 on: August 18, 2015, 07:50:19 pm »
If some part of the Lazarus IDE is not displaying properly, then your installation is faulty or corrupted somehow.
Uninstall your Lazarus. Clean (or delete) the installation directory(s) and start with a fresh installation of the latest release (version 1.4.2).

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: multithreading learning curve
« Reply #27 on: August 19, 2015, 03:55:25 am »
Wowser. I didn't realize that my attempted installation of Lazarus 1.2.0 was faulty. I'm a little scared but I will certainly take your advice and install Lazarus 1.4.2, though I suppose that means I'll have to reinstall a component or two. Using VLC seems to be a bit complicated since I think I'll have to also install LazActiveX and deal with late-binding, early-binding and other issues but I'll give it a try.

I wonder if the VLC component is better than MPlayerControl. I guess that I'll find out. Is there anyone out there who has used both MPlayer and VLC and can tell me which is better? MPlayer is okay but the quality isn't perfect. I wonder if VLC can do transparencies. Probably not.

Graeme

  • Hero Member
  • *****
  • Posts: 1527
    • Graeme on the web
Re: multithreading learning curve
« Reply #28 on: August 19, 2015, 09:56:05 am »
Using VLC seems to be a bit complicated since I think I'll have to also install LazActiveX and deal with late-binding, early-binding and other issues
When you say VLC you mean the video player right? I don't know which VLC components you are using, but the one I commissioned Michael van Canneyt to implement uses libVLC directly and doesn't require LazActiveX or anything like that. We use this VLC component in our commercial software and tested under Windows, Linux and FreeBSD, and it worked well. We also have GUI support for LCL and the fpGUI toolkit. We gave permission to include the core VLC component to be included with Free Pascal - this was back in 2012. See the "libvlc" directory in FCL - included with FPC 2.6.4 and later.

A GUI example app is included with fpGUI Toolkit and can be found at <fpgui>/examples/gui/video_vlc/. I've attached a screenshot of it running under FreeBSD 10.1 64-bit.

I believe there is a similar demo included with Lazarus too.
« Last Edit: August 19, 2015, 09:58:01 am by Graeme »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: multithreading learning curve
« Reply #29 on: August 20, 2015, 06:43:00 pm »
Graeme, it's a real privilege and inspiration to hear from you and any of the members of this forum. I know nothing about VLC other than knowing it plays videos, among other possible things. I was hoping that it has something similar to MPlayerControl that I could load at runtime on my main form.

If so, I would need to be able to play up to seven of them at the same time. I have downloaded VLC but I think it won't install correctly until I install a newer version of Lazarus. I plan to download and install Lazarus 1.4.2 as soon as I can.

Thank you very much for spending some of your valuable time in guiding me.

 

TinyPortal © 2005-2018