Restarting a thread

S

Salman Zari Ghanvi

I started two threads (thread1 and thread2) from main thread ... and after a
while thread1 stops.
Can I restart thread1 from thread2?

Actual problem:
I have started two threads from main thread. One is a real working thread
(thread1) and the other is a watchdog thread that watches thread1.isAlive
property.
If thread1 dies (because of throwing some exception), I want watchdog to
attempt a restart of thread1 for a few times before logging failure.
So .. can watchdog restart thread1? (remember they are siblings).

In thread1, is there a better way of handling exceptions (e.g. should it
suspend itself in case exceptions .. and let watchdog resume it.)? I read a
little about ManualResetEvent - Is it a better candidate for this stop/retry
situation.

Any other ideas to achieve the same?

Thanks.

SalmanZG
 
J

John Saunders

Salman Zari Ghanvi said:
I started two threads (thread1 and thread2) from main thread ... and after
a while thread1 stops.
Can I restart thread1 from thread2?

Actual problem:
I have started two threads from main thread. One is a real working thread
(thread1) and the other is a watchdog thread that watches thread1.isAlive
property.
If thread1 dies (because of throwing some exception), I want watchdog to
attempt a restart of thread1 for a few times before logging failure.
So .. can watchdog restart thread1? (remember they are siblings).

In thread1, is there a better way of handling exceptions (e.g. should it
suspend itself in case exceptions .. and let watchdog resume it.)? I read
a little about ManualResetEvent - Is it a better candidate for this
stop/retry situation.

Why restart the same thread? Why not start another thread running the same
code?

If the first thread died due to an uncaught exception, then you have no idea
what the state of its task is. You need to give up on it and start over
cleanly.

Also, you should consider listening to the AppDomain.UnhandledException
event. When you receive it, you can look at the exception which caused the
thread to terminate and determine whether or not you know what it means. If
you don't know what it means, then you should not start the new thread.
That's a good way to repeat bad things

You should also consider catching the exception closer to where it happened,
so that your code can do something about it.

John Saunders
 
J

Jon Skeet [C# MVP]

Salman Zari Ghanvi said:
I started two threads (thread1 and thread2) from main thread ... and after a
while thread1 stops.
Can I restart thread1 from thread2?
No.

Actual problem:
I have started two threads from main thread. One is a real working thread
(thread1) and the other is a watchdog thread that watches thread1.isAlive
property.
If thread1 dies (because of throwing some exception), I want watchdog to
attempt a restart of thread1 for a few times before logging failure.
So .. can watchdog restart thread1? (remember they are siblings).

A thread can't be restarted however it was created.
In thread1, is there a better way of handling exceptions (e.g. should it
suspend itself in case exceptions .. and let watchdog resume it.)? I read a
little about ManualResetEvent - Is it a better candidate for this stop/retry
situation.

That or using Monitor.Wait/Pulse would be fine, yes. Why can't it try
again itself though? What is the watchdog thread actually going to do?
 
S

Salman Zari Ghanvi

Thanks to both of you for your responses.
Can you recommend a good book for this topic (threading)?
 
S

Salman Zari Ghanvi

Thanks Jon,
I like that article.

Do you know if we can assign a thread/process to utilize a particular
processor on a multi-processor system?
 
J

Jon Skeet [C# MVP]

Salman Zari Ghanvi said:
Thanks Jon,
I like that article.

Do you know if we can assign a thread/process to utilize a particular
processor on a multi-processor system?

Not as far as I know, I'm afraid - although I could well have missed
something.
 
D

David Levine

I believe the win32 api has a setting for thread affinity but that API is
not supported in a .net thread.

Salman Zari Ghanvi said:
Thanks Jon,
I like that article.

Do you know if we can assign a thread/process to utilize a particular
processor on a multi-processor system?
 
L

Luc E. Mistiaen

Yes, that Win32 API exists, but as a .NET thread is not permanently
connected to the the same Win32 thread (the .NET framework multiplex all the
..NET thread on a fixed number of Win32 threads) this would not help.

The best you can do to be sure your .NET thread run on a single processor is
assign all the threads of an application domain to the same processor, but
is that desirable?

/LM

David Levine said:
I believe the win32 api has a setting for thread affinity but that API is
not supported in a .net thread.
 
D

David Levine

Luc E. Mistiaen said:
Yes, that Win32 API exists, but as a .NET thread is not permanently
connected to the the same Win32 thread (the .NET framework multiplex all
the .NET thread on a fixed number of Win32 threads) this would not help.

I am aware of this.
The best you can do to be sure your .NET thread run on a single processor
is assign all the threads of an application domain to the same processor,
but is that desirable?
I don't know of any .NET mechanism that would allow this - do you? However,
if you write your own host you could probably ensure this.

I am not sure why doing this would be desirable for a .NET app - the OS
usually makes pretty good decisions about scheduling threads. It can makes
sense for a high performance device driver to restrict itself to a single
processor to avoid the penalty of cache misses, but general purpose apps
usually don't benefit from second guessing the OS.
 
L

Luc E. Mistiaen

David Levine said:
I am not sure why doing this would be desirable for a .NET app - the OS
usually makes pretty good decisions about scheduling threads. It can makes
sense for a high performance device driver to restrict itself to a single
processor to avoid the penalty of cache misses, but general purpose apps
usually don't benefit from second guessing the OS.
I thinking exactly along the same lines...

/LM
 
J

Jon Skeet [C# MVP]

Luc E. Mistiaen said:
Yes, that Win32 API exists, but as a .NET thread is not permanently
connected to the the same Win32 thread (the .NET framework multiplex all the
.NET thread on a fixed number of Win32 threads)

<snip>

Does it actually do this now? I know it may well by .NET 2.0, and
there's nothing in the spec to stop it, but does the 1.1 CLR do this?
 
L

Luc E. Mistiaen

Jon Skeet said:
Does it actually do this now? I know it may well by .NET 2.0, and
there's nothing in the spec to stop it, but does the 1.1 CLR do this?
Don't know for sure. It is what I remember from a course I got a long time
ago (and it was 1.0 at the time...:)
 

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