471,334 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Dynamic wait on threads

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

Nov 29 '05 #1
4 1781
Prasad wrote:
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
Nov 29 '05 #2
Prasad wrote:
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

Nov 29 '05 #3
Prasad wrote:
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 30 '05 #4
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

Lasse Vågsæther Karlsen wrote:
Prasad wrote:
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2


Dec 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Guinness Mann | last post: by
2 posts views Thread by Vespasian | last post: by
9 posts views Thread by Roger Down | last post: by
11 posts views Thread by Peter Kirk | last post: by
12 posts views Thread by Perecli Manole | last post: by
16 posts views Thread by Thirsty Traveler | last post: by
2 posts views Thread by greyradio | last post: by
reply views Thread by rosydwin | 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.