Dynamic wait on threads

P

Prasad

Hello,

I have two threads T1 and T2.

T2 starts thread T1 and waits for 1 minute for T1 to finish (using
Join) and if it doesn't finish within that time, it aborts the thread
T1.

Now, I want to introduce dynamic wait in this. i.e. I want T1 to
communicate to T2 that it needs more time to finish and then
accordingly I want T2 to wait.

How do I accomplish this using C#? I thought off using thread local
storage for that. However, I don't see a way for thread T2 to access
the TLS of T1 from within T2.

Any help on this would be appreciated.

-Prasad
 
L

Lenard Gunda

Prasad said:
Hello,

I have two threads T1 and T2.

T2 starts thread T1 and waits for 1 minute for T1 to finish (using
Join) and if it doesn't finish within that time, it aborts the thread
T1.

Now, I want to introduce dynamic wait in this. i.e. I want T1 to
communicate to T2 that it needs more time to finish and then
accordingly I want T2 to wait.

How do I accomplish this using C#? I thought off using thread local
storage for that. However, I don't see a way for thread T2 to access
the TLS of T1 from within T2.

Any help on this would be appreciated.

-Prasad

You could create a variable that holds a counter - say how much more
time T1 needs. Both threads would need access to this variable.

Then use a synchronization object to lock() access to that variable. T1
would update it as it progresses, and T2 can check that if it needs to
wait more.


-Lenard
 
C

Chris Dunaway

Prasad said:
I have two threads T1 and T2.

Now, I want to introduce dynamic wait in this. i.e. I want T1 to
communicate to T2 that it needs more time to finish and then
accordingly I want T2 to wait.

I would recommend wrapping your T1 thread in a class and then add some
method or property that T2 could call to query T1 whether or not it
needs more time.

'Inside T2

If T1Class.QueryMoreTime() Then
'Do something
End If
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Prasad said:
Hello,

I have two threads T1 and T2.

T2 starts thread T1 and waits for 1 minute for T1 to finish (using
Join) and if it doesn't finish within that time, it aborts the thread
T1.

While not being an answer to your question, the other two have managed
that just fine, I would recommend you find a different solution to this
"aborts the thread" part.

Everything you do inside that tread basically needs to be wrapped in a
try/catch block to make sure you can react to the outside code forcibly
terminating it at the worst possible point.

What is it that this thread is doing? Wouldn't using an Event object or
similar be a better solution? I know that you wouldn't be able to
forcibly kill the thread no matter what it does, instead you would ask
the thread to stop doing whatever it is doing, and you'd have to check
this event at specific points inside the thread... But at least the
thread could be terminated properly, from the inside.
 
P

Prasad

Hello,

Thank you all for your responses. I used a mix of solution proposed by
Lenard and Chris and got it working.

While I agree with what Lasse has to say, I cannot use it directly at
this point. The reason is, T1 thread executes different code at
different times and its huge code. It's not feasible to fix all that
code to use the event object etc. Instead I opted for creating a
framework around T1 and T2 that uses thread local storage to
communicate time requirements through a shared data structure.

One question that remains unanswered is, "Is it possible to query TLS
of thread T1 from thread T2?"

Thanks.
-Prasad
 

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