Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Multithreaded code is ridiculous

Multithreaded code is ridiculous

Scheduled Pinned Locked Moved The Lounge
designalgorithmsregextutorialquestion
53 Posts 18 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Greg UtasG Greg Utas

    Well, I'm a longstanding member of the choir that you're preaching to. :laugh: If it's a thread pool, I have them share a work queue. You seem to imply that you queue messages against threads even in this case, but I doubt it. I only queue messages on a thread when it's the only one handling that kind of work.

    Robust Services Core | Software Techniques for Lemmings | Articles

    R Offline
    R Offline
    raddevus
    wrote on last edited by
    #19

    Greg Utas wrote:

    Well, I'm a longstanding member of the choir that you're preaching to.

    I agree. that's why if I had to do anything that really did heavy multi-threading (as a service or back-end type thing -- not just in a WinForm app) then I would use the new thing: Akka (which implements the Actor Model[^]. I've actually used it one time and it is quite amazing once you get past the learning curve. There's more on that landing page but read this quick summary that really is as good as it sounds.

    Akka site says:

    Actor Model The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.

    There are some nice simple diagrams there that show how it works.

    N Greg UtasG 2 Replies Last reply
    0
    • H honey the codewitch

      So as an instructional article I'm preparing code that can enqueue work items to a limited number of threads. If all the threads are busy and there's no more thread creation allowed (say you have a 3 thread limit) then one of the threads that's already busy enqueues the next message for when it's done with what it's currently processing. It schedules among the already busy threads using a round robin technique. The whole thing works using message passing and message queues. That's how the threads communicate with each other. You can post messages to each of the threads. The trouble with it is the complexity of it snowballs. All of the sudden I need to sync the UI which requires a whole separate layer. And then there's the interthread communication that's already complicated. There's only so much I can fit into an article without overwhelming the reader, and to produce anything approaching a real world example requires so much complicated code that it's just silly. Oh you did this over here? Well you need to synchronize over there. And because you did that, you need to handle it over there too, etc. It's a mess. I really think the approach traditional computers take to preemptive multithreading is an anti-pattern. It feels like every anti-pattern I've ever encountered: The more code you need to make it work, the more code you need to make it work! You end up putting more work into it just to get to the point where you can put more work into it, and everything feels like a workaround.

      Real programmers use butterflies

      R Offline
      R Offline
      RickZeeland
      wrote on last edited by
      #20

      Some languages are more suitable for concurrrent programming, see: programming-languages-for-concurrent-programming[^]

      1 Reply Last reply
      0
      • H honey the codewitch

        So as an instructional article I'm preparing code that can enqueue work items to a limited number of threads. If all the threads are busy and there's no more thread creation allowed (say you have a 3 thread limit) then one of the threads that's already busy enqueues the next message for when it's done with what it's currently processing. It schedules among the already busy threads using a round robin technique. The whole thing works using message passing and message queues. That's how the threads communicate with each other. You can post messages to each of the threads. The trouble with it is the complexity of it snowballs. All of the sudden I need to sync the UI which requires a whole separate layer. And then there's the interthread communication that's already complicated. There's only so much I can fit into an article without overwhelming the reader, and to produce anything approaching a real world example requires so much complicated code that it's just silly. Oh you did this over here? Well you need to synchronize over there. And because you did that, you need to handle it over there too, etc. It's a mess. I really think the approach traditional computers take to preemptive multithreading is an anti-pattern. It feels like every anti-pattern I've ever encountered: The more code you need to make it work, the more code you need to make it work! You end up putting more work into it just to get to the point where you can put more work into it, and everything feels like a workaround.

        Real programmers use butterflies

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #21

        Personally I find multithreading to be quite interesting and I don't have problems with it at all. Multiprocessing systems are also amusing. Throw in multiple processes each with multiple threads and it's great fun. Now I am working with CUDA and using literally thousands of threads. 2,304 on this laptop.

        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

        1 Reply Last reply
        0
        • R raddevus

          Greg Utas wrote:

          Well, I'm a longstanding member of the choir that you're preaching to.

          I agree. that's why if I had to do anything that really did heavy multi-threading (as a service or back-end type thing -- not just in a WinForm app) then I would use the new thing: Akka (which implements the Actor Model[^]. I've actually used it one time and it is quite amazing once you get past the learning curve. There's more on that landing page but read this quick summary that really is as good as it sounds.

          Akka site says:

          Actor Model The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.

          There are some nice simple diagrams there that show how it works.

          N Offline
          N Offline
          Nelek
          wrote on last edited by
          #22

          And what if it is not in .Net? ;) :rolleyes:

          M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

          R 1 Reply Last reply
          0
          • H honey the codewitch

            So as an instructional article I'm preparing code that can enqueue work items to a limited number of threads. If all the threads are busy and there's no more thread creation allowed (say you have a 3 thread limit) then one of the threads that's already busy enqueues the next message for when it's done with what it's currently processing. It schedules among the already busy threads using a round robin technique. The whole thing works using message passing and message queues. That's how the threads communicate with each other. You can post messages to each of the threads. The trouble with it is the complexity of it snowballs. All of the sudden I need to sync the UI which requires a whole separate layer. And then there's the interthread communication that's already complicated. There's only so much I can fit into an article without overwhelming the reader, and to produce anything approaching a real world example requires so much complicated code that it's just silly. Oh you did this over here? Well you need to synchronize over there. And because you did that, you need to handle it over there too, etc. It's a mess. I really think the approach traditional computers take to preemptive multithreading is an anti-pattern. It feels like every anti-pattern I've ever encountered: The more code you need to make it work, the more code you need to make it work! You end up putting more work into it just to get to the point where you can put more work into it, and everything feels like a workaround.

            Real programmers use butterflies

            N Offline
            N Offline
            Nelek
            wrote on last edited by
            #23

            I hope I didn't trigger this :rolleyes: ;P

            M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

            H 1 Reply Last reply
            0
            • M Mircea Neacsu

              Quote:

              o as an instructional article I'm preparing code that can enqueue work items to a limited number of threads. If all the threads are busy and there's no more thread creation allowed (say you have a 3 thread limit) then one of the threads that's already busy enqueues the next message for when it's done with what it's currently processing. It schedules among the already busy threads using a round robin technique.

              It's called a producer-consumer model and it's generally not so hard to handle. In most cases you need just one queue and all consumers feed from it. No rule that says a consumer cannot be a producer also. Consumers need to be polivalent meaning they know how handle any task that they pick from the queue. If you have task-oriented consumers that know how to do only one task (sometimes called fussy-eaters) you're beter off with multiple queues and and each consumer waits in the appropriate queue.

              Mircea

              H Offline
              H Offline
              honey the codewitch
              wrote on last edited by
              #24

              I figured out a better way to do it as I was coding it anyway. :)

              Real programmers use butterflies

              1 Reply Last reply
              0
              • N Nelek

                I hope I didn't trigger this :rolleyes: ;P

                M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #25

                You sort of did, but the truth is i was looking for something to code anyway. At any rate I made this: A Thread Pooling and Task Queuing Demonstration Using Message Passing[^] It's not exactly what you were looking for, i think? but it might be closer. :)

                Real programmers use butterflies

                N 1 Reply Last reply
                0
                • H honey the codewitch

                  You sort of did, but the truth is i was looking for something to code anyway. At any rate I made this: A Thread Pooling and Task Queuing Demonstration Using Message Passing[^] It's not exactly what you were looking for, i think? but it might be closer. :)

                  Real programmers use butterflies

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #26

                  Did you write all in less than 24 hours? :omg: :omg:

                  M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                  H 1 Reply Last reply
                  0
                  • R raddevus

                    Greg Utas wrote:

                    Well, I'm a longstanding member of the choir that you're preaching to.

                    I agree. that's why if I had to do anything that really did heavy multi-threading (as a service or back-end type thing -- not just in a WinForm app) then I would use the new thing: Akka (which implements the Actor Model[^]. I've actually used it one time and it is quite amazing once you get past the learning curve. There's more on that landing page but read this quick summary that really is as good as it sounds.

                    Akka site says:

                    Actor Model The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.

                    There are some nice simple diagrams there that show how it works.

                    Greg UtasG Offline
                    Greg UtasG Offline
                    Greg Utas
                    wrote on last edited by
                    #27

                    I'm guessing I've open-sourced something similar in C++. - Software Techniques for Lemmings[^] describes the general principles. - Robust C++: P and V Considered Harmful[^] describes how critical sections are minimized. - Session Processing Tutorial[^] describes the state machine framework, which might be a "double-click" on what some refer to as the Actor pattern. All these concepts are used in the call servers that run in AT&T's wireless network.

                    Robust Services Core | Software Techniques for Lemmings | Articles

                    <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                    <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                    1 Reply Last reply
                    0
                    • H honey the codewitch

                      So as an instructional article I'm preparing code that can enqueue work items to a limited number of threads. If all the threads are busy and there's no more thread creation allowed (say you have a 3 thread limit) then one of the threads that's already busy enqueues the next message for when it's done with what it's currently processing. It schedules among the already busy threads using a round robin technique. The whole thing works using message passing and message queues. That's how the threads communicate with each other. You can post messages to each of the threads. The trouble with it is the complexity of it snowballs. All of the sudden I need to sync the UI which requires a whole separate layer. And then there's the interthread communication that's already complicated. There's only so much I can fit into an article without overwhelming the reader, and to produce anything approaching a real world example requires so much complicated code that it's just silly. Oh you did this over here? Well you need to synchronize over there. And because you did that, you need to handle it over there too, etc. It's a mess. I really think the approach traditional computers take to preemptive multithreading is an anti-pattern. It feels like every anti-pattern I've ever encountered: The more code you need to make it work, the more code you need to make it work! You end up putting more work into it just to get to the point where you can put more work into it, and everything feels like a workaround.

                      Real programmers use butterflies

                      F Offline
                      F Offline
                      F ES Sitecore
                      wrote on last edited by
                      #28

                      Sounds useful. ThreadPool Class (System.Threading) | Microsoft Docs[^]

                      H 1 Reply Last reply
                      0
                      • N Nelek

                        And what if it is not in .Net? ;) :rolleyes:

                        M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                        R Offline
                        R Offline
                        raddevus
                        wrote on last edited by
                        #29

                        Nelek wrote:

                        And what if it is not in .Net?

                        Well, then, it is obviously in Java --> Akka: build concurrent, distributed, and resilient message-driven applications for Java and Scala | Akka[^] :rolleyes: :rolleyes: Yeah, if it's not in either of those two then you must do all the work...or remove the multi-threaded sections of the code. :laugh:

                        1 Reply Last reply
                        0
                        • F F ES Sitecore

                          Sounds useful. ThreadPool Class (System.Threading) | Microsoft Docs[^]

                          H Offline
                          H Offline
                          honey the codewitch
                          wrote on last edited by
                          #30

                          Yeah but there's issues using that, including not being able to control the number of threads it uses.

                          Real programmers use butterflies

                          F 1 Reply Last reply
                          0
                          • N Nelek

                            Did you write all in less than 24 hours? :omg: :omg:

                            M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                            H Offline
                            H Offline
                            honey the codewitch
                            wrote on last edited by
                            #31

                            Yes

                            Real programmers use butterflies

                            1 Reply Last reply
                            0
                            • H honey the codewitch

                              Yeah but there's issues using that, including not being able to control the number of threads it uses.

                              Real programmers use butterflies

                              F Offline
                              F Offline
                              F ES Sitecore
                              wrote on last edited by
                              #32

                              ThreadPool.SetMaxThreads(Int32, Int32) Method (System.Threading) | Microsoft Docs[^] ThreadPool.SetMinThreads(Int32, Int32) Method (System.Threading) | Microsoft Docs[^]

                              H 1 Reply Last reply
                              0
                              • F F ES Sitecore

                                ThreadPool.SetMaxThreads(Int32, Int32) Method (System.Threading) | Microsoft Docs[^] ThreadPool.SetMinThreads(Int32, Int32) Method (System.Threading) | Microsoft Docs[^]

                                H Offline
                                H Offline
                                honey the codewitch
                                wrote on last edited by
                                #33

                                I was wrong, fair enough but the other issue is I was demonstrating how thread pooling works, and using the ThreadPool and Task undermines all of that. I say as much at the beginning of the article.

                                Real programmers use butterflies

                                1 Reply Last reply
                                0
                                • H honey the codewitch

                                  So as an instructional article I'm preparing code that can enqueue work items to a limited number of threads. If all the threads are busy and there's no more thread creation allowed (say you have a 3 thread limit) then one of the threads that's already busy enqueues the next message for when it's done with what it's currently processing. It schedules among the already busy threads using a round robin technique. The whole thing works using message passing and message queues. That's how the threads communicate with each other. You can post messages to each of the threads. The trouble with it is the complexity of it snowballs. All of the sudden I need to sync the UI which requires a whole separate layer. And then there's the interthread communication that's already complicated. There's only so much I can fit into an article without overwhelming the reader, and to produce anything approaching a real world example requires so much complicated code that it's just silly. Oh you did this over here? Well you need to synchronize over there. And because you did that, you need to handle it over there too, etc. It's a mess. I really think the approach traditional computers take to preemptive multithreading is an anti-pattern. It feels like every anti-pattern I've ever encountered: The more code you need to make it work, the more code you need to make it work! You end up putting more work into it just to get to the point where you can put more work into it, and everything feels like a workaround.

                                  Real programmers use butterflies

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #34

                                  A single ConcurrentQueue (serial) or a ConcurrentBag (random) for messages, and a BackgroundWorker for dispatching worker threads and updating the UI (progress reporting event).

                                  It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                                  H 1 Reply Last reply
                                  0
                                  • L Lost User

                                    A single ConcurrentQueue (serial) or a ConcurrentBag (random) for messages, and a BackgroundWorker for dispatching worker threads and updating the UI (progress reporting event).

                                    It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                                    H Offline
                                    H Offline
                                    honey the codewitch
                                    wrote on last edited by
                                    #35

                                    I'm using a concurrent queue but i'm not using the BackgroundWorker nor the Task framework in my example, because the goal was to illustrate how the threading stuff works under the hood, not to provide production code. I say about as much in the introduction of the article.

                                    Real programmers use butterflies

                                    L 1 Reply Last reply
                                    0
                                    • H honey the codewitch

                                      I'm using a concurrent queue but i'm not using the BackgroundWorker nor the Task framework in my example, because the goal was to illustrate how the threading stuff works under the hood, not to provide production code. I say about as much in the introduction of the article.

                                      Real programmers use butterflies

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #36

                                      ConcurrentQueue is not exactly a "primitive". About as primitive as BGW, IMO. And BGW is very fine-grained.

                                      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                                      H 1 Reply Last reply
                                      0
                                      • L Lost User

                                        ConcurrentQueue is not exactly a "primitive". About as primitive as BGW, IMO. And BGW is very fine-grained.

                                        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                                        H Offline
                                        H Offline
                                        honey the codewitch
                                        wrote on last edited by
                                        #37

                                        I'm not saying it is. I'm simply saying that in order to demonstrate the whole shebang in a way that doesn't make the concepts specific to .NET required me to reinvent that particular wheel for demonstration purposes. Again I make it clear in the introduction that this approach is for demonstration, not for production, and suggest using the Task framework and the ThreadPool for this stuff.

                                        Real programmers use butterflies

                                        L 1 Reply Last reply
                                        0
                                        • H honey the codewitch

                                          I'm not saying it is. I'm simply saying that in order to demonstrate the whole shebang in a way that doesn't make the concepts specific to .NET required me to reinvent that particular wheel for demonstration purposes. Again I make it clear in the introduction that this approach is for demonstration, not for production, and suggest using the Task framework and the ThreadPool for this stuff.

                                          Real programmers use butterflies

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #38

                                          Saying it's "ridiculous" to start with is / was sort of invitation to fix it.

                                          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups