firing Multi-Threads sometimes misses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jake

    firing Multi-Threads sometimes misses

    I am new to multi-threading. Here is my scenario:

    foreach (<file in a certain folder>)
    new Thread((ThreadS tart)(delegate { processFile(<fi le>); })).Start();

    sometimes misses firing some threads to process files. It misses
    firing different threads every time I run it. I suppose it all
    depends on the time-slice it is getting at that moment (or I may be
    way off base here). What I mean by "misses" is that the "foreach"
    loop appears to have executed the thread-start line but the thread
    never actually starts.

    Can someone please tell me what I can add to the code to make sure
    that a thread is started and not missed?

    Checking a flag that a thread sets when it first starts is an obvious
    solution, but I am looking for a more language native way (if one
    exists).

    Your help is greatly appreciated.
    jake
  • pudchuck

    #2
    Re: firing Multi-Threads sometimes misses

    I've never seen a thread not start. I have seen a thread encounter an
    exception and die silently. Are your threads starting and dying
    without you noticing? Maybe put a big try/catch block in the thread
    start routine and log unhandled exceptions? To answer your question,
    this may work:

    if ( thread.ThreadSt ate == ThreadState.Uns tarted ) { freak out... }


    I'm not here to critique your implementation, but starting an
    unbounded number of threads can kill an application. I've done it by
    accident. Can you feed a list of tasks to a single worker thread or
    the thread pool instead?

    On Jul 23, 10:05 am, jake <jakedim...@gma il.comwrote:
    I am new to multi-threading. Here is my scenario:
    >
    foreach (<file in a certain folder>)
    new Thread((ThreadS tart)(delegate { processFile(<fi le>); })).Start();
    >
    sometimes misses firing some threads to process files. It misses
    firing different threads every time I run it. I suppose it all
    depends on the time-slice it is getting at that moment (or I may be
    way off base here). What I mean by "misses" is that the "foreach"
    loop appears to have executed the thread-start line but the thread
    never actually starts.
    >
    Can someone please tell me what I can add to the code to make sure
    that a thread is started and not missed?
    >
    Checking a flag that a thread sets when it first starts is an obvious
    solution, but I am looking for a more language native way (if one
    exists).
    >
    Your help is greatly appreciated.
    jake

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: firing Multi-Threads sometimes misses

      On Jul 23, 5:05 pm, jake <jakedim...@gma il.comwrote:
      I am new to multi-threading.  Here is my scenario:
      >
      foreach (<file in a certain folder>)
              new Thread((ThreadS tart)(delegate { processFile(<fi le>); })).Start();
      >
      sometimes misses firing some threads to process files.  It misses
      firing different threads every time I run it.  I suppose it all
      depends on the time-slice it is getting at that moment (or I may be
      way off base here).  What I mean by "misses" is that the "foreach"
      loop appears to have executed the thread-start line but the thread
      never actually starts.
      I strongly suspect that's not true. I suspect what *actually* happens
      is that some files are processed by two different threads.

      Change your code to take a local copy (within the foreach loop) of the
      iteration variable, and use *that* in your anonymous method. Otherwise
      the iteration variable itself is captured, and that may well have
      moved onto the next value before the thread starts.

      Jon

      Comment

      • jake

        #4
        Re: firing Multi-Threads sometimes misses

        Thank you Jon. That makes sense. There are some sparse but strange
        log file entries (during file processing) that I can now attribute to
        the cause you just mentioned that I, admittedly, did not think about.
        I will implement the local variable copy solution you mentioned.
        Regards,
        jake


        On Jul 23, 12:46 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
        On Jul 23, 5:05 pm, jake <jakedim...@gma il.comwrote:
        >
        I am new to multi-threading. Here is my scenario:
        >
        foreach (<file in a certain folder>)
        new Thread((ThreadS tart)(delegate { processFile(<fi le>); })).Start();
        >
        sometimes misses firing some threads to process files. It misses
        firing different threads every time I run it. I suppose it all
        depends on the time-slice it is getting at that moment (or I may be
        way off base here). What I mean by "misses" is that the "foreach"
        loop appears to have executed the thread-start line but the thread
        never actually starts.
        >
        I strongly suspect that's not true. I suspect what *actually* happens
        is that some files are processed by two different threads.
        >
        Change your code to take a local copy (within the foreach loop) of the
        iteration variable, and use *that* in your anonymous method. Otherwise
        the iteration variable itself is captured, and that may well have
        moved onto the next value before the thread starts.
        >
        Jon

        Comment

        • jake

          #5
          Re: firing Multi-Threads sometimes misses

          Jon,
          That was the cause. I created a local copy of the iteration variable
          and I passed that to the anonymous method, just like you said, and it
          worked.
          Thanks again.
          jake


          On Jul 23, 12:46 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
          On Jul 23, 5:05 pm, jake <jakedim...@gma il.comwrote:
          >
          I am new to multi-threading. Here is my scenario:
          >
          foreach (<file in a certain folder>)
          new Thread((ThreadS tart)(delegate { processFile(<fi le>); })).Start();
          >
          sometimes misses firing some threads to process files. It misses
          firing different threads every time I run it. I suppose it all
          depends on the time-slice it is getting at that moment (or I may be
          way off base here). What I mean by "misses" is that the "foreach"
          loop appears to have executed the thread-start line but the thread
          never actually starts.
          >
          I strongly suspect that's not true. I suspect what *actually* happens
          is that some files are processed by two different threads.
          >
          Change your code to take a local copy (within the foreach loop) of the
          iteration variable, and use *that* in your anonymous method. Otherwise
          the iteration variable itself is captured, and that may well have
          moved onto the next value before the thread starts.
          >
          Jon

          Comment

          Working...