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. General Programming
  3. C#
  4. Is this a bad way to multithread?

Is this a bad way to multithread?

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • J Offline
    J Offline
    Joshua Lunsford
    wrote on last edited by
    #1

    Thread[] workerThread = new Thread[someInt]; for(int i = 0; i < someInt; i++) { string keyInfo = someKeyInfo().ToString(); workerThread[i] = new Thread(new ThreadStart(DoSomeWork(keyInfo))); workerThread[i].Start(); } foreach(Thread t in workerThread) { while(t.IsAlive()){} }

    S R A 4 Replies Last reply
    0
    • J Joshua Lunsford

      Thread[] workerThread = new Thread[someInt]; for(int i = 0; i < someInt; i++) { string keyInfo = someKeyInfo().ToString(); workerThread[i] = new Thread(new ThreadStart(DoSomeWork(keyInfo))); workerThread[i].Start(); } foreach(Thread t in workerThread) { while(t.IsAlive()){} }

      S Offline
      S Offline
      stbaker
      wrote on last edited by
      #2

      I didn't think called methods could have parameters in Framework 1.1. The methods used by ThreadStart must be static.

      R 1 Reply Last reply
      0
      • J Joshua Lunsford

        Thread[] workerThread = new Thread[someInt]; for(int i = 0; i < someInt; i++) { string keyInfo = someKeyInfo().ToString(); workerThread[i] = new Thread(new ThreadStart(DoSomeWork(keyInfo))); workerThread[i].Start(); } foreach(Thread t in workerThread) { while(t.IsAlive()){} }

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        Depending on the number of threads you could consider using ThreadPool. Also you should rethink the IsAlive loop. First of all it could consume much processor time so you should at least do:

        foreach(Thread t in workerThread)
        while(t.IsAlive())
        Thread.Sleep(10);

        Instead of waiting for all threads separately you could also use a counter which counts the started and terminated threads. Have a look at the Interlocked[^] class for a sample.

        1 Reply Last reply
        0
        • S stbaker

          I didn't think called methods could have parameters in Framework 1.1. The methods used by ThreadStart must be static.

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          First point is correct but .Net 2.0 has a new overload for the Thread constructor accepting a ParametrizedThreadStart. Second point isn't true even under 1.1 also 'normal' functions can be used.

          1 Reply Last reply
          0
          • J Joshua Lunsford

            Thread[] workerThread = new Thread[someInt]; for(int i = 0; i < someInt; i++) { string keyInfo = someKeyInfo().ToString(); workerThread[i] = new Thread(new ThreadStart(DoSomeWork(keyInfo))); workerThread[i].Start(); } foreach(Thread t in workerThread) { while(t.IsAlive()){} }

            S Offline
            S Offline
            stbaker
            wrote on last edited by
            #5

            Robert is right, just don't go thread crazy. Also, I don't know if it's still an issue on uniprocessor machines but the child thread won't start unless you call Thread.Sleep(0) in the parent after creating it.

            1 Reply Last reply
            0
            • J Joshua Lunsford

              Thread[] workerThread = new Thread[someInt]; for(int i = 0; i < someInt; i++) { string keyInfo = someKeyInfo().ToString(); workerThread[i] = new Thread(new ThreadStart(DoSomeWork(keyInfo))); workerThread[i].Start(); } foreach(Thread t in workerThread) { while(t.IsAlive()){} }

              A Offline
              A Offline
              Andy Brummer
              wrote on last edited by
              #6

              If you are going to do something like that use:

              foreach(Thread t in workerThread)
              {
              thread.Join();
              }

              I'd only do something like that in a simple console application and I didn't care about the results of the DoSomeWork method. Otherwise I'd use asyncronous delegates and callback methods.


              I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

              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