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

My intent was to perform most message passing by way of queues and use thread signaling for tasks that don't lend themselves to that approach. If the general concept (as contrasted with implementation) is flawed, I love to get set straight.

In the abstract, it doesn't make sense (to me, in isolation of your ultimate goals), to have a thread who's only purpose in life is to read from the keyboard and then queue that input to another thread to action. Especially when that other thread already has other things to do which, on the basis of what you've told me so far, have no inherent relationship to the input (command) actions.

You say that your "intent was to perform most message passing by way of queues" but the command input only becomes a "message", if you queue it. If you do not queue it, the input is simply input that needs to be actioned. And that can be done immediately by the reading thread as I've shown above.

One of the most basic principles of message passing architectures is that you do not poll. If you are going to poll, you are better off using an event loop architecture--though that is rarely a good idea. A message agent should just wait on its input queue until there is something for it to do, and then do it and go back to waiting. Your implementation above where the same agent is polling two queues completely defeats that basic principle.

But reading between the lines of your post, your ultimate goals seem to be very different from the code/descriptions that you've posted so far. In that respect, it is difficult, and probably pointless, to make further recommendations.

If you have a description of the goals and requirements of what you are hoping to achieve, then it might be possible to suggest an architecture that lends itself to development towards those goals, as a starting point.


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^6: threads on Windows
by kennethk (Abbot) on Feb 17, 2009 at 22:45 UTC

    I've thought about what you've said and played with code for a few days. And I come up with some new material, posted below. I have multiple threads, which I call king, minister, worker, and media. The king (the main thread) makes proclamations, i.e. takes keyboard input and sticks it on the input queue. The minister implements these by sitting on the input queue, deciding policy and delegating to workers or sticking errors on the output queue. The worker threads are unchanged from the code you posted a few nodes up. All output is handled by the media thread, which sits on the output queue. In this way, I tried to keep the message agents (minister and media) working only on one queue. If this does gain a GUI, the king would be where this would be implemented.

    There are a few problems I see with what I have written:

    1. There's metadata problem. While the current system only has minimal information flow through the system, a more complex problem is likely to have a significant amount of information that won't matter to the intensive computation but must maintain its connection to maintain context. I can see two clear solutions - pass an object through the entire system (Minister->worker->Media) or create shared hashes between Minister and Media. I'm inclined toward the former from an aesthetic perspective.
    2. There's a small risk in the status command of locking the minister thread if the target is paused. I tried to restructure things to minimize that risk, but it's still non-zero.
    3. I tried to keep the command codes localized, but I had to modify the king loop condition from $minister->is_running to not /^q/ to avoid having to enter an extraneous <enter> to exit the script.

    As a final note, if I do not explicitly close all thread-inappropriate channels before proceeding, I get blocking behavior between the <> in the King thread and the new thread generation in the Minister thread. I still do not grok why this happens - if you know of any good materials that discuss how I/O is implemented on a MS system, particularly with regard to Perl behaviors, I'd appreciate a shove in that direction. I hate solutions that say "Just don't do that".

    Any wisdom you care to share would be appreciated.

      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.

        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.