Thread.join

T

Tony Johansson

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
 
A

Arne Vajhøj

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
 
M

Mike Schilling

Arne Vajhøj said:
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()
{
}
}
}
 
T

Tony Johansson

Mike Schilling said:
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
 
W

Willem van Rumpt

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()
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

Arne said:
[...]
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
 

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