firing Multi-Threads sometimes misses

  • Thread starter Thread starter jake
  • Start date Start date
J

jake

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

foreach (<file in a certain folder>)
new Thread((ThreadStart)(delegate { processFile(<file>); })).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
 
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.ThreadState == ThreadState.Unstarted ) { 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?
 
I am new to multi-threading.  Here is my scenario:

foreach (<file in a certain folder>)
        new Thread((ThreadStart)(delegate { processFile(<file>); })).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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

timer/threading 10
threading 2
Thread 1
CRT thread-safe when called from C-Sharp 2
threading 6
2.0 Threads, Join() and Invoke 3
Threading and Treeviews 5
stopping threads 2

Back
Top