in reply to Re^6: threads on Windows
in thread threads on Windows

It's still not clear to me what benefit you think you are getting from passing the inputs to another thread to be actioned?

I don't yet have a multi-core machine to test my hypothysis, but I strongly suspect that even if your minister thread is currently running on a another core when you read and post the input to the queue, that it probably takes an order of magnitude longer for the code to get actioned than if you did it inline within the read loop.

If you are wedded to your "king's don't do things for themselves" analogy, then just call your minister subroutine directly, rather messing around with an extra thread and queue.

I don't doubt it will work okay your way, I just don't see any benefit coming from the extra complexity and the performance hit it entails?

I'm also unconvinced about the need for semaphores and signals in your workers. I see what your doing with them, and that it works, but still the question remains: why? Why do you need to pause/resume/kill threads?

So far, the whole exercise seems to be a case of: "There are these modules kicking around, I wonder what I can do with them."; rather than: "I have this problem to solve, how can I best achieve that.". Ie. You seem to be trying to develop and infrastructure to cater to some supposed possible requirements, rather trying to code a solution to a specific problem.

In every case where I've seen a framework developed in the absence of a real and well specified requirement, the framework has ended up being overengineered, inefficient and a nightmare to use for real applications, because it was built to satisfy some theoretical design goal rather than real application requirements.

For example: Kings rarely instruct ministers; rather they simply endorse the proposals put forward by ministers. And kings certainly don't answer there own phones--or whatever anology you use for STDIN. And shouldn't you have a few secretaries around to do the donkey work?

I really don't think that I can be of any further help to you until you describe the actual requirements of your real application, rather than some badly fitting analogy.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^8: threads on Windows
by kennethk (Abbot) on Feb 18, 2009 at 16:38 UTC

    If the following post is just more line noise, I won't be offended if you choose not to reply.

    The assumed benefit is creation of an abstract API. The performance hit taken with the extra thread should be relatively negligible because tasks should be few and relatively long running. The minister and workers can be packaged in a library module while the king and media would be interface specific. The program I'm hoping to get updated is proprietary (and written in VB...) so I can't really show you examples. The talk around the group is to not only have the desktop version but also a simplified subscription-based web version, thus my desire for abstraction. And in any case, much of this was a case of "I wonder what I can do with them" to gain familiarity with Perl's thread syntax and capabilities before I put in a formal proposal.

      The assumed benefit is creation of an abstract API.

      The problem is, whether you action inputs in the current thread, or in another thread has nothing to do with abstraction: it is an implementation detail.

      And abstraction might be something like a subroutine called: enact(). It might be used something like:

      while( my $decree = <STDIN> ) { chomp $decree; enact( $decree ); }

      You can now implement enact() is any number of ways: as immediate code as an if/elsif/else cascade, or a dispatch table; or passing the responsibility off to a concurrent thread or process via a queue, pipe, socket or shared memory. Whatever.

      But as currently coded, there is no abstraction. (Other than the use of a queue and another thread, and they add complexity rather than hide it!) And you are gaining no benefit from the use of concurrency. It will take at least 2 context switches to go from reading an input to the point of having enacted it. And a context switch will take 2 or 3 orders of magnitude longer (x100 or x1000) than processing the input inline.

      All you've achieved by multithreading that data path, is to use more memory and make things slower. Now given that all of the inputs you've outlined can be processed far more quickly than a human being can type the next command, there is no benefit there either. You would only benefit if the inputs were being supplied by another program or computer faster than you could process them inline. And even then, unless the input stream was bursty, all you would do is fill up the queue with commands faster than you could take them off and process them.

      Voltaire (apparently) said: a book is not finished when you can add no more, but rather when you can remove no more. And nothing is a more laudable and beneficial goal in code development, than removing anything that serves no purpose.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I think I follow your arguments here and appreciate their content. By using a direct function call, I can maintain the same level of abstraction (even to the point of not modifying the format of my messages as they are today, if I so desired) without the added complexity and performance penalties that a separate minister thread would incur. If I needed some level of persistence (in this case, the thread and semaphore hashes), that could be created using module variables.

        This raises a question for me about output. In this scenario, I can rationalize the use of an outbox queue since I have results coming in from multiple sources that then need to be pushed in some manner to the interface. However, I also see that my appreciation of the use of threaded tools is not yet mature (kid with a new toy). As I see it, it would be a good idea to keep the worker threads isolated to their tasks and use of a dispatch table would cause their threads of control to get caught up in the interface. Does this make sense, or am I missing the big picture again?

        Thank you for your time, patience and wisdom for helping me on my pilgrimage toward enlightenment.