Recent

Author Topic: multithreading learning curve  (Read 21799 times)

Graeme

  • Hero Member
  • *****
  • Posts: 1526
    • Graeme on the web
Re: multithreading learning curve
« Reply #30 on: August 20, 2015, 06:53:37 pm »
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.
Yes you can. VLC media player includes the libVLC library (a .SO or .DLL file). In fact the desktop VLC media player itself is simply a GUI front-end for the libVLC library. A much more complete front-end that the screenshot I posted ;), but nevertheless the same in principal. With the code included in FPC, you should be able to create multiple instances and tell them which canvases in the GUI to use for the display output. I've never had such a need, but technically I can't see any issues doing that.

As for your comment about an updated Lazarus - I don't know why that would be required. Like I said, the code was donated to FPC and Lazarus back in 2012, so any Lazarus version since then should work.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: multithreading learning curve
« Reply #31 on: August 21, 2015, 11:14:50 pm »
I'm sure some folks might be getting tired of seeing this thread but can someone tell me the steps I need to take in  in Lazarus to create and use a VLC component? I've installed the player but I don't know the syntax or the commands to use with the library to create an instance of a VLC component.

I made a path to the VLC libraries and put libvlc and vlc in the "uses" section but that won't compile. I looked through the examples and searched the web and this forum but didn't find what I need. I don't want any skins or forms to show. I just want a box where I can play a video like MPlayerComponent.

When I did a Google search for "lazarus how to use vlc" I got references to LazActiveX (which uses VLC) and then found out that I have to download some Lazarus trunk components because it doesn't come with Lazarus 1.0.10. This is all very confusing.

Graeme

  • Hero Member
  • *****
  • Posts: 1526
    • Graeme on the web
Re: multithreading learning curve
« Reply #32 on: August 24, 2015, 12:27:28 pm »
For LCL
Open the <lazarus>/components/vlc/lazvlc.lpk package and compile it.

Then open the <lazarus>/components/vlc/test/testlcl.lpi project and compile and see if it works.

For fpGUI
Open the <fpgui>/examples/gui/video_vlc/testfpguivlc.lpi project and compile and see if it works.

In both cases, if the example projects don't compile or give some error, please copy and paste the exactly error output. I can't help you if you just say... it doesn't work.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

derek.john.evans

  • Guest
Re: multithreading learning curve
« Reply #33 on: August 24, 2015, 01:15:24 pm »
For LCL
Open the <lazarus>/components/vlc/lazvlc.lpk package and compile it.

Unsure if that works on Lazarus 1.4.2, because in reglazvlc there is:
Code: [Select]
{$IF FPC_FULLVERSION<20701}
{$ERROR needs at least FPC 2.7.1}
{$ENDIF}   
Which is strange considering Lazarus 1.4.2 is released with FPC 2.6.4

What works for me is. Add the LazVLC path to your projects "other unit files": eg:
Code: [Select]
C:\lazarus\components\vlc\
Add lclvlc to your uses list. (not libvlc or vlc)

Create the VLC component (its not a control) at runtime:
Code: [Select]
  var FLCLVLCPlayer: TLCLVLCPlayer;   // << This should be in your main form.

  FLCLVLCPlayer := TLCLVLCPlayer.Create(Self);
  FLCLVLCPlayer.ParentWindow := Self;  // This is the control that displays the video.

And then play a video using:
Code: [Select]
FLCLVLCPlayer.PlayFile('e:\Robin Williams full live performance in Washington.mp4');     
« Last Edit: August 24, 2015, 01:25:09 pm by derek.john.evans »

sam707

  • Guest
Re: multithreading learning curve
« Reply #34 on: August 24, 2015, 03:56:34 pm »
multithreading learning curve =

My advice is => replace synchronize (and polling) with 'events" as much as possible

http://www.paradicesoftware.com/blog/2014/02/dont-use-suspend-and-resume-but-dont-poll-either/

TEventObect let a thread waitfor another (you can use an array of events in each thread to wait for many others), ...
TCriticalSection makes sure that one and only one thread at time executes enclosed portion of code of the Critical section. (enclosed with TcriticalSection.Aquire and TCriticalSection.Release)

Synchronize should be used ONLY when main thread needs to do stuffs with partial results (display progress for example) of other threads  (like GUI updates) in most of the cases synchronize is a bottleneck call that drastically slows down your applications (the thread calls main one and suspends itself until end of main thread tasks called by the synchronize method). So be careful with synchronise, it can eventually kill the benefit of threads use
« Last Edit: August 24, 2015, 04:40:33 pm by sam707 »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: multithreading learning curve
« Reply #35 on: August 26, 2015, 06:26:28 am »
multithreading learning curve =

My advice is => replace synchronize (and polling) with 'events" as much as possible

http://www.paradicesoftware.com/blog/2014/02/dont-use-suspend-and-resume-but-dont-poll-either/

TEventObect let a thread waitfor another (you can use an array of events in each thread to wait for many others), ...
TCriticalSection makes sure that one and only one thread at time executes enclosed portion of code of the Critical section. (enclosed with TcriticalSection.Aquire and TCriticalSection.Release)

Synchronize should be used ONLY when main thread needs to do stuffs with partial results (display progress for example) of other threads  (like GUI updates) in most of the cases synchronize is a bottleneck call that drastically slows down your applications (the thread calls main one and suspends itself until end of main thread tasks called by the synchronize method). So be careful with synchronise, it can eventually kill the benefit of threads use
I do agree with avoiding synchronize at all costs but can you give me some sample of the events? I'm most interested on the subject. Thank you.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vincococka

  • Full Member
  • ***
  • Posts: 101
Re: multithreading learning curve
« Reply #36 on: August 26, 2015, 08:17:18 am »
---
  Guide me God and I`ll find you

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: multithreading learning curve
« Reply #37 on: August 26, 2015, 08:57:59 am »
Here is small tutorial to Events :

http://www.paradicesoftware.com/blog/2014/02/dont-use-suspend-and-resume-but-dont-poll-either/
I'm sorry I was not clear enough. I am familiar with the synchronization primitives, I'm interesting in sam707's use case on those primitives. The idea is to get as many details as he is comfortable sharing on the implementations and possible traps to avoid.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vincococka

  • Full Member
  • ***
  • Posts: 101
Re: multithreading learning curve
« Reply #38 on: August 26, 2015, 10:05:59 am »
It was little bit surprising why were you asking for this kind of information :)
---
  Guide me God and I`ll find you

sam707

  • Guest
Re: multithreading learning curve
« Reply #39 on: August 27, 2015, 12:39:18 am »
there is simple template to use events to spare 100% of cpu usage in my preceding post while puting a thread on wait state instead of suspended or polling that consumes resources and cpu registers contexts switching. (see link in the post)

I personnally use this kind of template with minimal efforts and little modifications so that the working thread sends messages to main thread and forms upon eventS

http://wiki.freepascal.org/Multithreaded_Application_Tutorial#Using_SendMessage.2FPostMessage_to_communicate_between_threads

coupling events, messages, and critical sections in your own needed way is not a big deal, and permits to avoid at 99% the heavy 'synchronize" method

example =>

thread = wait for an event to be set (while in execution loop, waitfor timeout=0 so theres no delay just a check each loop pass)

main form (in main thread so) sets the event at some point

thread catch the event state, posts a message to main form and resets evnt

interea main form received a message and displays progress of the thread stord in a TLMessage record (or from thread properties, read only for other threads) while the thread continues working and did never suspended unlike with synchronize
http://www.freepascal.org/docs-html/fcl/syncobjs/teventobject.waitfor.html
« Last Edit: August 27, 2015, 01:35:19 am by sam707 »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: multithreading learning curve
« Reply #40 on: August 27, 2015, 01:22:50 am »
there is simple template to use events to spare 100% of cpu usage in my preceding post while puting a thread on wait state instead of suspended or polling that consumes resources and cpu registers contexts switching. (see link in the post)

I will search the older posts on this thread to get the details, thank you.


I personnally use this kind of template with minimal efforts and little modifications so that the working thread sends messages to main thread and forms upon eventS

http://wiki.freepascal.org/Multithreaded_Application_Tutorial#Using_SendMessage.2FPostMessage_to_communicate_between_threads

coupling events, messages, and critical sections in your own needed way is not a big deal, and permits to avoid at 99% the heavy 'synchronize" method

example =>

thread = wait for an event to be set (while in execution loop, waitfor timeout=0 so theres no delay)

main form (in main thread so) sets the event at some point

thread catch the event state, sends message to main form and resets evnt

interea main form received a message and displays progress of the thread stord in a TLMessage record (or from thread properties, read only) while the thread continues working and did never suspended unlike with synchronize
http://www.freepascal.org/docs-html/fcl/syncobjs/teventobject.waitfor.html
I'm guessing this is to eliminate pooling from the main thread. One question why not simple post a message to the main thread when the data are ready? Is this some sort of "don't waste time unless its required" kind of thing?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

sam707

  • Guest
Re: multithreading learning curve
« Reply #41 on: August 27, 2015, 01:40:55 am »
well it depends on your application... I made a TTimer and periodically checks progression of a HUGE loop. but you are right on tiny ones!

what is good to know about Events and furthermore Named Events, is that they are GLOBALS thru the whole running OSes. meaning you can catch/set/reset their states inter threads, inter processes, and inter applicationS when you know their names

http://www.freepascal.org/docs-html/fcl/syncobjs/teventobject.html

PSecurityattribs are useful only on windows, i keep it to nil, and it works crossplatform
« Last Edit: August 27, 2015, 01:43:56 am by sam707 »

sam707

  • Guest
Re: multithreading learning curve
« Reply #42 on: August 27, 2015, 01:48:31 am »
application A creates a TeventObect named "My_App"

application B creates a TeventObject named "My_App"

App A set the event

App B found the event is set, then knows App A is actually running on the OS

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: multithreading learning curve
« Reply #43 on: August 27, 2015, 02:35:38 am »
well it depends on your application... I made a TTimer and periodically checks progression of a HUGE loop. but you are right on tiny ones!

Just to clarify when I say "when the data are ready" I do not mean when the loop have finished its job I mean the data to communicate, those can be a percentage complete every 10 seconds for example instead of having a timer in the main form that asks for a completion report every 10 seconds you simple post a completion report message from the thread every 10 seconds this has the added feature that you can add a small "inform list" in the thread and have multiple forms/controls receive the message.

Just a heads up if you decide to test this method in small intervals you will see the side effect of the form processing messages long after the thread has finished looping

what is good to know about Events and furthermore Named Events, is that they are GLOBALS thru the whole running OSes. meaning you can catch/set/reset their states inter threads, inter processes, and inter applicationS when you know their names

http://www.freepascal.org/docs-html/fcl/syncobjs/teventobject.html

PSecurityattribs are useful only on windows, i keep it to nil, and it works crossplatform
Nice! Didn't had the need to synchronize processes and threads from different processes yet but its good to know it can be done.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018