firing Multi-Threads sometimes misses

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
 
P

pudchuck

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?
 
J

Jon Skeet [C# MVP]

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
 
J

jake

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
 
J

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

Top