in reply to Re^2: Multithreading, how to?
in thread Multithreading, how to?

I do like the imagery though. Despite the fact that a kindergarten teacher may be able to get a group of 5 year olds to work together to accomplish some relatively impressive things, that does not mean that any random adult can do the same.

The guy who mentored me in multi-threading suggested an analogy that I've always found useful. Writing multi-threaded code is to writing single-threaded code as playing a musical instrument is to conducting an orchestra. A single musician can be a little sloppier and ad-lib quite a bit without ruining the result. That doesn't work with an orchestra. (You could argue it does work with a good Jazz band.) This is not to say that individuals are sloppier, but a single person does not have the coordination issues that an orchestra does. (Disclaimer: I am not a musician and don't even play one on the Net.)

The point is not that you should never use threads, but that organization and coordination become more important.

The suggestion that you try multi-threading in a smaller experiment before applying it to a big system does not seem to be FUD to me. I would give the same advice to someone trying out a new programming language, methodology, framework, or any other piece of technology.

I suspect that much of the FUD about threaded code out there is spread by people who jumped in with both feet, rather than doing a test program. At least, that has been my experience with some who are fanatically against any form of threading.

G. Wade

Replies are listed 'Best First'.
Re^4: Multithreading, how to?
by BrowserUk (Patriarch) on Dec 31, 2008 at 19:29 UTC

    Again, I don't disagree with you with regard to threading in general.

    I do like the imagery though. Despite the fact that a kindergarten teacher may be able to get a group of 5 year olds to work together to accomplish some relatively impressive things, that does not mean that any random adult can do the same.

    The problem with this analogy, as with the musicians, is that programs do not have minds of their own; and so are not subject to variable skill levels, PMS, hangovers, distraction by the musician in front's flimsy or tight clothing, or any of the other factors that can distract and disrupt human beings.

    With code, once the right steps are codified and verified, the computer will happily perform those steps over and over precisely without getting bored, tired or distracted. The same does not hold true for human beings.

    Once a piece of code, say a subroutine, is tested to work correctly as a single threaded, standalone environment, it will continue to work correctly when you run multiple copies of it concurrently. Provided that there are no unintentional interactions between the copies.

    And it is in exactly that last area where iThreads are pretty much unique relative to most other forms of threading. The core concept that distinguishes them is that you have to explicitely arrange for anything to be shared. If you share nothing, there can be--barring errors in the core implementation; which do crop up occasionally in this field just as they do in every other field--no accidental interactions.

    That single fact alone makes iThreads different from most other forms of threading.

    The second major flaw in the multiple persons analogy is that with few exceptions, the success of human group activieties is dependant upon the management, coordination and timing of the interactions between the individuals. Hence the 'teacher' & 'conductor' roles in those analogies.

    Whilst there are tasks that can benefit from multitasking that require such a coordinating and synchronising role, there are also whole classes of applications that do not. Collectively termed data-parallel, for which applying iThreads multi-threading to well-tested, single-threaded code requires minimal changes and (almost) no knowledge of the deep voodoo that is required to use other forms of threading.

    I wrote my first threaded code--under OS/2--circa the late 80's under OS/2, and have made most of the mistakes--often multiple times! I've been involved in both successful and unsuccessful projects that used extensive kernel threading, but using iThreads has taught me things that I never seen described anywhere in the literature.

    The most important of those lessons is that the easiest way to avoid the well-known problems of deadlocking, priority inversion et al. is to simply avoid using the mechanisms that lead to them. It sounds too simple to be true, but in the vast majority of the use cases I have explored, I've found relatively simple and reliable solutions that use iThreads, Queues, minimal shared data and minimal locking.

    And in the few non-perl threaded applications I've been involved in since getting familiar with iThreads, I've found that lesson transfers quite well to other languages like C(++), Clean and more.


    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.
      With code, once the right steps are codified and verified, the computer will happily perform those steps over and over precisely without getting bored, tired or distracted. The same does not hold true for human beings.

      However, even in the single threaded case, code can appear to work for a long time (potentially years) and eventually fail because it was not coded correctly. While the code may not get bored or distracted, changes in its environment can make it fail intermittently. This is, of course independent of concurrency considerations.

      If you share nothing, there can be--barring errors in the core implementation; which do crop up occasionally in this field just as they do in every other field--no accidental interactions.

      Unless I misunderstand, though the separate threads still share the same file system, sockets, environment, etc. many of which are subject to cause interactions if the programmer is not careful. If I'm understanding the issue correctly, iThreads make memory non-shared by default. While the threads are more isolated this way, they are not completely independent.

      The second major flaw in the multiple persons analogy is that with few exceptions, the success of human group activieties is dependant upon the management, coordination and timing of the interactions between the individuals. Hence the 'teacher' & 'conductor' roles in those analogies.

      I obviously missed something in my explanation. I was not seeing the conductor or teacher as portions of the threaded code, the programmer is in the position of the 'teacher' or 'conductor' in these analogies.

      Let me try another way.

      In most single threaded programs, the programmer can lay out a sequence of steps with appropriate control logic. You can pretty much see how the code will run from beginning to end. (More complicated problems make this harder to see, but bear with me for a moment.)

      In a multi-threaded program, on the other hand, the programmer builds smaller, independent sequences of steps that run under their own control. Any place that these threads need to interact is not under the direct control of the programmer. So the programmer is kind of one step removed from the code. This makes understanding the interactions and timing (for lack of a better word) more important in this style than in single threaded code.

      It's been my experience that many people find this harder. Or they code assuming that things will behave the way they want (like it did in the single threaded model). These people are often stumped when their code doesn't work.

      Obviously, the fewer the interactions between the threads (not accessing shared resources and such), the less the coordination issues will affect us. If there's no interaction between the threads, then I wonder why they are in the same program.

      The most important of those lessons is that the easiest way to avoid the well-known problems of deadlocking, priority inversion et al. is to simply avoid using the mechanisms that lead to them. It sounds too simple to be true, but in the vast majority of the use cases I have explored, I've found relatively simple and reliable solutions that use iThreads, Queues, minimal shared data and minimal locking.

      These are the lessons I also learned from multi-threading work, even without iThreads from the mid 90s. But, these approaches do not always come naturally when someone tries threads for the first time.

      I guess that is what I've been trying to say (but have only done so badly). Multi-threading and single threading benefit from somewhat different viewpoints and tool sets. Someone doing threads for the first time should be aware that the different tools and viewpoints may be needed.

      G. Wade

        I hope you don't object to me continuing this discussion? I find debate the best way for me to clarify my own views and opinions--and you debate well.

        That said, I'm going to be lazy here and summarise(*) your post above.

        (* I mean 'precisé', but I'm unsure of the correct spelling. I know that last grapheme is incorrect, but I don't know how to produce (what I think) is the correct one.)

        I'll summarise it as: "Threading is hard, especially for beginners. It will require you to think differently and use different tools. You may find it easier not to try."

        That is an unfair simplification of the position you have expressed in the preceding posts. But not by much.

        Programmers have an inherent bias that leads them to believe and express that what they do is hard. It makes sense. It is good for business, and good for salaries. But ostensibly, relative to other technical fields, it is entirely wrong. Or at least, overstated.

        Learning to drive is a radical departure from anything you are likely to have done before you do it. It doesn't just require hand-eye coordination, but rather hand-eye-foot-brain coordination. But more importantly, it requires anticipation of the actions and reactions of others. Whilst some other activities--sports; computer games etc--exhibit similar requirements, the difference is that mistakes when driving are not just sometimes lethal, but frequently so.

        By contrast, programming is essentially benign. Barring the extremes--nuclear power plants; weapon systems; medical equipment--programming errors are seldom lethal. And even in those extreme cases where they can be, they can in most cases be mitigated by proper and conscientious testing.

        The point I'm trying to make is that everyone has to start somewhere. And regardless of the complexities of the task undertaken by a programmer, there is always a learning curve involved. But unlike many other fields of endeavour, programmers have the inherent benefit of being able to test the effects of their skills, prior to putting them into potentially lethal practice.

        Sure, threads are different. Threading can be hard. But neither fact should be lauded as a reason to preclude anyone from trying to acquire the requisite skills.


        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.