Hi Guy,

On Dec 6, 2017, at 12:39, Guy Berdnikov <isai1...@gmail.com> wrote:
> *First place: winpr/libwinpr/pool/pool.c*
> 
> static BOOL InitializeThreadpool(PTP_POOL pool)
This function really launches four threads to get started...

> *Second place: **winpr/libwinpr/pool/pool.c*
> 
> BOOL winpr_SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic)
> In each of these places it creates 4 threads (8 in total) in the same
> pool and with the same thread routine function
But then winpr_SetThreadpoolThreadMinimum() does not add four more
threads. It adds more only if the required minimum is greater than
the number of currently running threads.

> What this routine function does is WaitingForMultipleObjects, in our case 2
> events
> 1) *TerminateEvent* - which happens when the program is terminated.
> 2) *QueueEvent* - which I don't understand what it is waiting for.
> 
> So my question is, why are these 8 threads created and what are they
> waiting for at this *QueueEvent?*
Well, this is a pretty clean-cut implementation of the Thread Pool
pattern [1] which usually involves a set of worker threads coupled
with a work queue shared between them.

All threads run the same code which continuously pulls work from the
queue and then performs it. The actual 'work' is abstracted out from
the thread pool so the threads call some opaque function and pass it
some opaque argument. Thus you can submit your work into the pool and
it will be executed by the first free thread. This allows to easily
parallelize data processing by splitting the whole work into
independent chunks and submitting them as separate work items.

You cannot safely terminate a thread while it's still running, so in
order to destroy a thread pool you need to ensure that all worker
threads are not doing any work. This is achieved by posting a special
termination event into queue which signals the threads that there is
no more work for them to do and never will be, so they can terminate.

[1]: 
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686760(v=vs.85).aspx
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
FreeRDP-devel mailing list
FreeRDP-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freerdp-devel

Reply via email to