Hello!
As I understand the join method on a thread object you must have at least
two threads where one thread is waiting for another thread to complete this
mean calling join on the main thread will cause the main thread to hang
waiting for itself which is not possible.
So is my understanding correct here ?
//Tony
|
|
0
|
|
|
|
Reply
|
Tony
|
6/17/2010 10:14:22 PM |
|
On 17-06-2010 18:14, Tony Johansson wrote:
> As I understand the join method on a thread object you must have at least
> two threads where one thread is waiting for another thread to complete this
> mean calling join on the main thread will cause the main thread to hang
> waiting for itself which is not possible.
>
> So is my understanding correct here ?
t.Join() causes the current thread to wait until t has
terminated.
I have not tested what happen if t=current thread - either
it will hang forever or you will get an exception because
Join actually checks.
Given that you should never do this, then it is not so
interesting.
Arne
|
|
0
|
|
|
|
Reply
|
ISO
|
6/18/2010 12:27:51 AM
|
|
"Arne Vajh�j" <arne@vajhoej.dk> wrote in message
news:4c1abd86$0$286$14726298@news.sunsite.dk...
> On 17-06-2010 18:14, Tony Johansson wrote:
>> As I understand the join method on a thread object you must have at least
>> two threads where one thread is waiting for another thread to complete
>> this
>> mean calling join on the main thread will cause the main thread to hang
>> waiting for itself which is not possible.
>>
>> So is my understanding correct here ?
>
> t.Join() causes the current thread to wait until t has
> terminated.
>
> I have not tested what happen if t=current thread - either
> it will hang forever or you will get an exception because
> Join actually checks.
>
Oddly, while Join checks for threads that have not been started, it does not
check for the current thread, and will in fact hang forever.
using System;
using System.Threading;
namespace Tests
{
class TestJoin
{
public static void Main()
{
try
{
Thread s = new Thread(new ThreadStart(doIt));
s.Join();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Thread.CurrentThread.Join();
}
static void doIt()
{
}
}
}
|
|
0
|
|
|
|
Reply
|
Mike
|
6/18/2010 6:27:41 AM
|
|
Mike Schilling wrote:
> [...]
> Oddly, while Join checks for threads that have not been started, it does
> not check for the current thread, and will in fact hang forever.
Indeed. I have even seen code that uses that fact intentionally to keep
a thread alive. The "technique" is to call Thread.Join() after the
thread's done whatever setup it needs to. Then it blocks there until
some other thread aborts it with the Thread.Abort() method.
Not a code design I'd condone, but it's been done. :)
|
|
0
|
|
|
|
Reply
|
Peter
|
6/18/2010 6:47:56 AM
|
|
"Mike Schilling" <mscottschilling@hotmail.com> skrev i meddelandet
news:eJDfd9qDLHA.4400@TK2MSFTNGP05.phx.gbl...
>
>
> "Arne Vajh�j" <arne@vajhoej.dk> wrote in message
> news:4c1abd86$0$286$14726298@news.sunsite.dk...
>> On 17-06-2010 18:14, Tony Johansson wrote:
>>> As I understand the join method on a thread object you must have at
>>> least
>>> two threads where one thread is waiting for another thread to complete
>>> this
>>> mean calling join on the main thread will cause the main thread to hang
>>> waiting for itself which is not possible.
>>>
>>> So is my understanding correct here ?
>>
>> t.Join() causes the current thread to wait until t has
>> terminated.
>>
>> I have not tested what happen if t=current thread - either
>> it will hang forever or you will get an exception because
>> Join actually checks.
>>
>
> Oddly, while Join checks for threads that have not been started, it does
> not check for the current thread, and will in fact hang forever.
>
> using System;
> using System.Threading;
>
> namespace Tests
> {
> class TestJoin
> {
> public static void Main()
> {
> try
> {
> Thread s = new Thread(new ThreadStart(doIt));
> s.Join();
> }
> catch (Exception ex)
> {
> Console.WriteLine(ex);
> }
>
> Thread.CurrentThread.Join();
> }
>
> static void doIt()
> {
> }
> }
> }
The code that you passed do not cause the Thread to hang you will catch the
exception in the exception clause.
I don't understand what you mean with "Oddly, while Join checks for threads
that have not been started, it does not
check for the current thread, and will in fact hang forever"
But in fact as I wrote it wil not hang
//Tony
|
|
0
|
|
|
|
Reply
|
Tony
|
6/18/2010 7:15:21 AM
|
|
On 18-6-2010 9:15, Tony Johansson wrote:
>
> The code that you passed do not cause the Thread to hang you will catch the
> exception in the exception clause.
> I don't understand what you mean with "Oddly, while Join checks for threads
> that have not been started, it does not
> check for the current thread, and will in fact hang forever"
>
> But in fact as I wrote it wil not hang
>
> //Tony
>
It will and it does.
If you step through the program you will see exactly what the statement
"Oddly, while Join checks for threads that have not been started, it
does not check for the current thread, and will in fact hang forever"
Means:
An exception thrown at s.Join(), and a hang on Thread.CurrentThread.Join()
|
|
0
|
|
|
|
Reply
|
Willem
|
6/18/2010 9:23:12 AM
|
|
On 18-06-2010 03:15, Tony Johansson wrote:
> "Mike Schilling"<mscottschilling@hotmail.com> skrev i meddelandet
> news:eJDfd9qDLHA.4400@TK2MSFTNGP05.phx.gbl...
>> "Arne Vajh�j"<arne@vajhoej.dk> wrote in message
>> news:4c1abd86$0$286$14726298@news.sunsite.dk...
>>> On 17-06-2010 18:14, Tony Johansson wrote:
>>>> As I understand the join method on a thread object you must have at
>>>> least
>>>> two threads where one thread is waiting for another thread to complete
>>>> this
>>>> mean calling join on the main thread will cause the main thread to hang
>>>> waiting for itself which is not possible.
>>>>
>>>> So is my understanding correct here ?
>>>
>>> t.Join() causes the current thread to wait until t has
>>> terminated.
>>>
>>> I have not tested what happen if t=current thread - either
>>> it will hang forever or you will get an exception because
>>> Join actually checks.
>>>
>>
>> Oddly, while Join checks for threads that have not been started, it does
>> not check for the current thread, and will in fact hang forever.
>>
>> using System;
>> using System.Threading;
>>
>> namespace Tests
>> {
>> class TestJoin
>> {
>> public static void Main()
>> {
>> try
>> {
>> Thread s = new Thread(new ThreadStart(doIt));
>> s.Join();
>> }
>> catch (Exception ex)
>> {
>> Console.WriteLine(ex);
>> }
>>
>> Thread.CurrentThread.Join();
>> }
>>
>> static void doIt()
>> {
>> }
>> }
>> }
>
> The code that you passed do not cause the Thread to hang you will catch the
> exception in the exception clause.
> I don't understand what you mean with "Oddly, while Join checks for threads
> that have not been started, it does not
> check for the current thread, and will in fact hang forever"
>
> But in fact as I wrote it wil not hang
If you add the missing:
s.Start();
then it will hang!
Arne
|
|
0
|
|
|
|
Reply
|
ISO
|
6/21/2010 2:10:59 AM
|
|
Arne Vajh�j wrote:
> [...]
>>> using System;
>>> using System.Threading;
>>>
>>> namespace Tests
>>> {
>>> class TestJoin
>>> {
>>> public static void Main()
>>> {
>>> try
>>> {
>>> Thread s = new Thread(new ThreadStart(doIt));
>>> s.Join();
>>> }
>>> catch (Exception ex)
>>> {
>>> Console.WriteLine(ex);
>>> }
>>>
>>> Thread.CurrentThread.Join();
>>> }
>>>
>>> static void doIt()
>>> {
>>> }
>>> }
>>> }
>>
>> The code that you passed do not cause the Thread to hang you will
>> catch the exception in the exception clause. [...]
>
> If you add the missing:
>
> s.Start();
>
> then it will hang!
It will hang regardless, and not on the "s.Join()" statement (which will
throw an exception in the original code as intended, or will simply
proceed normally when the started thread has exited as proposed by
you�either way, no hang).
The call to Join() on the CurrentThread cannot ever return, because the
CurrentThread cannot exit until the call to Join() returns.
Pete
|
|
0
|
|
|
|
Reply
|
Peter
|
6/21/2010 5:20:25 AM
|
|
On 21-06-2010 01:20, Peter Duniho wrote:
> Arne Vajh�j wrote:
>> [...]
>>>> using System;
>>>> using System.Threading;
>>>>
>>>> namespace Tests
>>>> {
>>>> class TestJoin
>>>> {
>>>> public static void Main()
>>>> {
>>>> try
>>>> {
>>>> Thread s = new Thread(new ThreadStart(doIt));
>>>> s.Join();
>>>> }
>>>> catch (Exception ex)
>>>> {
>>>> Console.WriteLine(ex);
>>>> }
>>>>
>>>> Thread.CurrentThread.Join();
>>>> }
>>>>
>>>> static void doIt()
>>>> {
>>>> }
>>>> }
>>>> }
>>>
>>> The code that you passed do not cause the Thread to hang you will
>>> catch the exception in the exception clause. [...]
>>
>> If you add the missing:
>>
>> s.Start();
>>
>> then it will hang!
>
> It will hang regardless,
You are correct. It catches that exception and continue.
Arne
|
|
0
|
|
|
|
Reply
|
windows
|
6/22/2010 2:42:02 AM
|
|
|
8 Replies
620 Views
(page loaded in 0.286 seconds)
|