471,317 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

firing Multi-Threads sometimes misses

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
Jul 23 '08 #1
4 1168
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?

On Jul 23, 10:05 am, jake <jakedim...@gmail.comwrote:
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
Jul 23 '08 #2
On Jul 23, 5:05*pm, jake <jakedim...@gmail.comwrote:
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
Jul 23 '08 #3
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.comwrote:
On Jul 23, 5:05 pm, jake <jakedim...@gmail.comwrote:
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
Jul 23 '08 #4
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.comwrote:
On Jul 23, 5:05 pm, jake <jakedim...@gmail.comwrote:
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
Jul 24 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by qaz | last post: by
6 posts views Thread by Rod Snyder | last post: by
3 posts views Thread by Mike | last post: by
reply views Thread by John Grandy | last post: by
3 posts views Thread by Dean Earley | last post: by
19 posts views Thread by furiousmojo | last post: by
6 posts views Thread by Robert | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.