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. Other Discussions
  3. The Weird and The Wonderful
  4. Multithreading done "right"

Multithreading done "right"

Scheduled Pinned Locked Moved The Weird and The Wonderful
performancequestion
24 Posts 14 Posters 0 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.
  • P PIEBALDconsult

    I think that makes sense in a WinForms app -- if GenerateTimeLineImage takes a bit of time, which seems likely.

    F Offline
    F Offline
    Fedor Hajdu
    wrote on last edited by
    #8

    It's not a winforms app, but even it were, it still don't make sense. GenerateTimeLineImage is a heavy function but it would still block the UI thread (if that's why you mentioned WinApp) because of the m_thread.Join() there.

    1 Reply Last reply
    0
    • P PIEBALDconsult

      I think that makes sense in a WinForms app -- if GenerateTimeLineImage takes a bit of time, which seems likely.

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

      No, not the least. In that case the UI thread would be waiting until the other thread is finally done. Meanwhile the UI would not respond, just as if the task had been done on the UI thread itself.

      At least artificial intelligence already is superior to natural stupidity

      P 1 Reply Last reply
      0
      • L Lost User

        No, not the least. In that case the UI thread would be waiting until the other thread is finally done. Meanwhile the UI would not respond, just as if the task had been done on the UI thread itself.

        At least artificial intelligence already is superior to natural stupidity

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #10

        Thread.Join Method Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.

        B 1 Reply Last reply
        0
        • P PIEBALDconsult

          Thread.Join Method Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #11

          Does this actually work? (I.e. do your event handlers get called, does the window repaint properly, etc?)

          U 1 Reply Last reply
          0
          • B BobJanova

            Does this actually work? (I.e. do your event handlers get called, does the window repaint properly, etc?)

            U Offline
            U Offline
            User 8592736
            wrote on last edited by
            #12

            No. Try the following code - The ui blocks for 10 seconds - i am sure.

            private void button1_Click(object sender, EventArgs e)
            {
            Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
            m_thread.Start();
            m_thread.Join();
            }

            void GenerateTimeLineImage()
            {
            Thread.Sleep(10000);
            }

            P 1 Reply Last reply
            0
            • U User 8592736

              No. Try the following code - The ui blocks for 10 seconds - i am sure.

              private void button1_Click(object sender, EventArgs e)
              {
              Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
              m_thread.Start();
              m_thread.Join();
              }

              void GenerateTimeLineImage()
              {
              Thread.Sleep(10000);
              }

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #13

              OK, I sit corrected. However, maybe the developer often needs to do that with several items:

              Thread t1 = ... ; t1.Start() ;
              Thread t2 = ... ; t2.Start() ;
              ...
              Thread tn = ... ; tn.Start() ;

              Join (t1 ) ;
              Join (t2 ) ;
              ...
              Join (tn ) ;

              and uses this as a general pattern. :shrug: In which case I might consider a method that takes a collection of delegates:

              private static void
              InvokeAll
              (
                params System.Threading.ThreadStart\[\] Items
              )
              {
                /\* Null checking  :D  \*/
              
                System.Threading.Thread\[\] thread = new System.Threading.Thread \[ Items.Length \] ;
              
                for ( int i = 0 ; i < Items.Length ; i++ )
                {
                  if ( Items \[ i \] != null )
                  {
                    thread \[ i \] = new System.Threading.Thread ( Items \[ i \] ) ;
                    thread \[ i \].Start() ;
                  }
                }
              
                for ( int i = 0 ; i < thread.Length ; i++ )
                {
                  if ( thread \[ i \] != null )
                  {
                    thread \[ i \].Join() ;
                  }
                }
              
                return ;
              }
              

              (Untested)

              1 Reply Last reply
              0
              • F Fedor Hajdu

                I recently started working for a new company and found quite a few situations like this:

                Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
                m_thread.Start();
                m_thread.Join();

                I'm no expert on mulithreading but there are so many different (not just brainless copy-paste) of code like that, that it made me wondering is there some hidden magic behind that code that I'm not aware of. So, what's the difference between that and

                GenerateTimeLineImage();

                besides obvious performance loss due to creating a pointless thread?

                G Offline
                G Offline
                Gary Wheeler
                wrote on last edited by
                #14

                The only reason I can imagine doing something like that is if there is a requirement that GenerateTimeLineImage() execute on a non-UI thread, and yet be synchronized to the UI thread that requests the operation. There are plenty of the opposite circumstance (requesting a UI operation from a worker thread), but this sounds strange.

                Software Zen: delete this;

                P 1 Reply Last reply
                0
                • G Gary Wheeler

                  The only reason I can imagine doing something like that is if there is a requirement that GenerateTimeLineImage() execute on a non-UI thread, and yet be synchronized to the UI thread that requests the operation. There are plenty of the opposite circumstance (requesting a UI operation from a worker thread), but this sounds strange.

                  Software Zen: delete this;

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #15

                  Or perhaps to protect the UI thread from Exceptions? :confused:

                  A B J 3 Replies Last reply
                  0
                  • P PIEBALDconsult

                    Or perhaps to protect the UI thread from Exceptions? :confused:

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #16

                    Or perhaps there is another thread that somehow manages these extra threads (e.g., abort them if they take too long).

                    Thou mewling ill-breeding pignut!

                    B 1 Reply Last reply
                    0
                    • A AspDotNetDev

                      Or perhaps there is another thread that somehow manages these extra threads (e.g., abort them if they take too long).

                      Thou mewling ill-breeding pignut!

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #17

                      The Thread object is local so I don't think this can be happening.

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Or perhaps to protect the UI thread from Exceptions? :confused:

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #18

                        That's even worse since it's effectively a really obfuscated empty catch block!

                        1 Reply Last reply
                        0
                        • F Fedor Hajdu

                          I recently started working for a new company and found quite a few situations like this:

                          Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
                          m_thread.Start();
                          m_thread.Join();

                          I'm no expert on mulithreading but there are so many different (not just brainless copy-paste) of code like that, that it made me wondering is there some hidden magic behind that code that I'm not aware of. So, what's the difference between that and

                          GenerateTimeLineImage();

                          besides obvious performance loss due to creating a pointless thread?

                          M Offline
                          M Offline
                          Member 7679313
                          wrote on last edited by
                          #19

                          Call me a nice guy who generally gives people the benefit of the doubt, but this looks like code where someone INTENDED for some operations to occur on separate threads asynchronously, put some of the plumbing in, put a thread.join in for synchronous debugging, and never got back to it. You are correct in that the code would be more performant without creating the additional thread as they presently are, but the context of the usage is also important in determining your predecessor's intent. If GenerateTimeLineImage() is a void function that is basically a "fire and forget" service call, commenting out m_thread.Join() might increase application performance, and by quite a bit. I'll tell you that there is no hidden magic, but you haven't provided enough context for me to be as condemning as some other folks here.

                          L 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            Or perhaps to protect the UI thread from Exceptions? :confused:

                            J Offline
                            J Offline
                            jschell
                            wrote on last edited by
                            #20

                            PIEBALDconsult wrote:

                            Or perhaps to protect the UI thread from Exceptions?

                            I suspect that would probably be a misunderstanding of exceptions. If the thread code doesn't catch exceptions then it will terminate the application, thus certainly impacting the UI. And if it does catch exceptions then just the same as wrapping the method call in a try/catch.

                            1 Reply Last reply
                            0
                            • M Member 7679313

                              Call me a nice guy who generally gives people the benefit of the doubt, but this looks like code where someone INTENDED for some operations to occur on separate threads asynchronously, put some of the plumbing in, put a thread.join in for synchronous debugging, and never got back to it. You are correct in that the code would be more performant without creating the additional thread as they presently are, but the context of the usage is also important in determining your predecessor's intent. If GenerateTimeLineImage() is a void function that is basically a "fire and forget" service call, commenting out m_thread.Join() might increase application performance, and by quite a bit. I'll tell you that there is no hidden magic, but you haven't provided enough context for me to be as condemning as some other folks here.

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

                              Member 7679313 wrote:

                              Call me a nice guy who generally gives people the benefit of the doubt, but this looks like code where someone INTENDED for some operations to occur on separate threads asynchronously, put some of the plumbing in, put a thread.join in for synchronous debugging, and never got back to it.

                              Ok, I call you a nice guy :) He did write that he found this in serveral places in the code, wich makes some kind of accident less likely. I would just take a look at some other projects he worked on and if something like that also appears there, then there is little room left for doubt.

                              At least artificial intelligence already is superior to natural stupidity

                              1 Reply Last reply
                              0
                              • F Fedor Hajdu

                                I recently started working for a new company and found quite a few situations like this:

                                Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
                                m_thread.Start();
                                m_thread.Join();

                                I'm no expert on mulithreading but there are so many different (not just brainless copy-paste) of code like that, that it made me wondering is there some hidden magic behind that code that I'm not aware of. So, what's the difference between that and

                                GenerateTimeLineImage();

                                besides obvious performance loss due to creating a pointless thread?

                                K Offline
                                K Offline
                                KP Lee
                                wrote on last edited by
                                #22

                                If GenerateTimeLineImage is managing multiple threads, then the code makes sense. If not, then the coder read something, didn't understand it, but thought it was "cool", without understanding what the method was for.

                                1 Reply Last reply
                                0
                                • F Fedor Hajdu

                                  He's not, I'm his replacement... :)

                                  S Offline
                                  S Offline
                                  spencepk
                                  wrote on last edited by
                                  #23

                                  That explains everything :)

                                  1 Reply Last reply
                                  0
                                  • F Fedor Hajdu

                                    I recently started working for a new company and found quite a few situations like this:

                                    Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
                                    m_thread.Start();
                                    m_thread.Join();

                                    I'm no expert on mulithreading but there are so many different (not just brainless copy-paste) of code like that, that it made me wondering is there some hidden magic behind that code that I'm not aware of. So, what's the difference between that and

                                    GenerateTimeLineImage();

                                    besides obvious performance loss due to creating a pointless thread?

                                    F Offline
                                    F Offline
                                    Florin Jurcovici
                                    wrote on last edited by
                                    #24

                                    If there's version control in place, have a look - it might be that previous versions had some code between .Start() and .Join().

                                    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